A query is a question about your data that allows you to retrieve specific information from your database. It is an instruction to the database system to search for a particular set of data that is of interest to you, and to present it in a useful format.
A query is written as an SQL SELECT statement, as in the following example of a fairly complex query that finds the number of customers in each city in New York or California that has at least one customer. The result is sorted by state and city.
SELECT St, City, Count(C_no) FROM Customer WHERE St="CA" OR City="New York" GROUP BY St, City HAVING count(*) > 0 ORDER BY St, City
The SELECT statement consists of several important clauses.
Lists the columns to be retrieved from the table(s).
Lists the tables from which data is to be retrieved.
Consists of a set of conditions that the rows of data must satisfy.
Organizes data into subsets or groups based on the values in the grouped columns. It is frequently used to create summary rows containing totals, sums, counts, averages, minimum values, and maximum values.
Defines conditions that summary rows of data must satisfy.
Specifies the order in which the rows of data should be sorted.