January 22, 2020
3min Read
Herawan
In regular English, a query means a request for information. What is a query in computer programming then? Simple, it’s the same – except the information is retrieved from a database. This is handy for data manipulation – adding, removing, and changing data. That’s how we’ll use this word in the article.
However, you don’t just type in a random ‘request.’ You write your query based on a set of pre-defined code, so your database understands the instruction. We refer to this code as the query language.
The standard for database management is Structured Query Language (SQL). Remember, SQL is different from MySQL: the former is the query language, the latter is the software which uses the language. While it is true that SQL is the most popular choice among database software, it is definitely not the only one. Several others are AQL, Datalog, and DMX.
Regardless, these languages make database communication easy.
Getting started with website development? We offer fully-supported web hosting plans for small business and young bright-minded developers.
Let’s say that you want to order an Americano at Starbucks. You make a request by saying “Can I have an Americano?”. The Barista will understand the meaning of your request and give you the ordered item.
A query works the same way. It gives meaning to the code used in any query language. Be it SQL or anything else, both the user and the database can always exchange information as long as they ‘speak’ the same language.
You may now think that placing a query is the only way to request data. In fact, quite a few database software options let you use other methods. The most popular ones are:
Now that we know quite a bit about queries, why not try writing some? Please be aware that for this exercise we use SQL.
Data source: Participant (Table Name)
Let’s say that you collected some data from a survey. Below is a snippet of your data.
ID | Name | Sex | Age | Occupation |
1 | John | Male | 17 | Student |
2 | Peter | Male | 26 | Unemployed |
3 | Margareth | Female | 34 | Teacher |
4 | Lea | Female | 34 | Unemployed |
With SQL, you can write either SQL SELECT or SQL ACTION queries. As the name suggests, SQL SELECT lets you select parts of your data, and SQL ACTION enables you to perform action-oriented operations like deleting and inserting data, updating values, and creating new tables. Well, examples speak louder than words, let’s have a look:
Name | Occupation |
John | Student |
Peter | Unemployed |
Margareth | Teacher |
Lea | Unemployed |
ID | Name | Sex | Age | Occupation |
1 | John | Male | 17 | Student |
3 | Margareth | Female | 34 | Teacher |
ID | Name | Sex | Age | Occupation |
1 | John | Male | 17 | Student |
2 | Peter | Male | 26 | Unemployed |
3 | Margareth | Female | 34 | Teacher |
4 | Lea | Female | 34 | Unemployed |
5 | Mario | Male | 67 | Retired |
ID | Name | Sex | Age | Occupation |
1 | John | Male | 17 | Student |
2 | Peter | Male | 26 | Unemployed |
3 | Margareth | Female | 34 | Headmaster |
4 | Lea | Female | 34 | Unemployed |
Some SQL statements, like the ones above, are a solid testament to what a query language can do. It lets you deal with your data more effectively. Imagine if you have thousands of data rows. Manipulating them doesn’t have to be hard. Besides, most query languages are intuitive, meaning that they are easy to learn once you understand some basic rules. Let’s get our hands dirty with database queries and deal with our data like pros!
June 18 2020
A very great explanation. thank you <3
July 26 2020
very good, thanks.
Leave a reply