What is a Database Query?

People create a database because they want to organize the data in such a way that they can glean useful information from it. These data can be accessed by using a query. A query, if properly stated, will extract data from the database. For example, a businessman creates a database which contains the list of his products, their prices, and the number of people who bought them. If he wants to find out which products are most expensive, he can input a query into the database through the select statement, and the data he is looking for will be given to him. Likewise, if he wants to find out which products are most popular among the customers, he will have to input a different query.

The SELECT Statement

You can input a query into the database by using the SELECT statement. It extracts data from the multitude of tables and presents only those that the user is looking for. For it to work, the user must add other elements or clauses since SELECT is not a standalone statement. There are two types of clauses, the required ones and the optional ones. The required clauses are absolutely needed for the SELECT statement to work, but the optional ones only enhance its functionality. For example, the FROM clause is a clause that is ‘required’ and should therefore be included in SELECT statements. FROM is an element or a clause that is always included in every SELECT statement. IT tells the database which tables to access to answer the query. It always denotes at least one table. Other important clauses include WHERE and ORDER BY.

What is a Database Query? database query

AND or OR

AND or OR are called operators. They are used to denote conditions that further specify which data the user is looking for. To make things clearer, let’s use an example. Let’s say that a businessman wants to find the items in his inventory that have not been sold more than three times and costs more than twenty dollars from tables 1 to 4. Of course, he will issue a SELECT statement with the FROM clause saying that tables 1 to 4 will be searched. Next he will add the WHERE clause which specifies that the conditions, price>$20 AND sales<3. So, when the results are given, those rows which did not meet the conditions specified will be omitted.

Sorting

Now, let’s say that the same businessman want to see which items are most impractical to buy. That is, he wants to find out which items in his inventory are most expensive and are least likely to be bought. He can do this by using the ORDER BY clause. By default, all the results are displayed in alphabetical order. But the user can define whether to display the data in ascending or descending order. Of course, he’ll have to specify which column to use in sorting out the items. For example, if the business man simply specifies ‘descending’, the products will simply be listed from Z-A. But if he specifies that column two will be used, then the prices will be used for sorting.

With proper use of the SELECT function, the user can make more complicated queries, thus, gleaning more information.



3 thoughts on “What is a Database Query?

  1. Pingback: Database Optimization | Database Guides

  2. Pingback: IBM DB2 Database | Database Guides

  3. Pingback: What is High Availability? | Database Guides