Query Definition

Queries Harness the Power of Your Database

A database query requests data, or inserts data, into a database. A query must be written in the language the database requires, usually SQL.

For example, when you want some data from a database, you use a query to request the specific information you want. Perhaps you have an Employee table and you want to track sales performance numbers. You might "query" your database for the employee that recorded the highest sales in a given period.

The SQL SELECT Statement

A database query must follow the query format required by the database. The most common format is the Structured Query Language (SQL) standard query format used by many database management systems. SQL is a powerful language capable of very advanced queries. 

Fun fact: Even database experts disagree on how to pronounce SQL. Some say "SEQUEL," while other insist that the proper pronunciation is to say each letter, as in "S-Q-L."

SQL uses a SELECT statement to "select" specific data.

Let's consider an example based on the commonly used Northwind database, which frequently ships with database products as a tutorial.

Here's an excerpt from the database's Employees table: 

Excerpt from the Northwinds database Employees table
EmployeeIDLastNameFirstNameTitleAddressCityRegion
1DavolioNancySales Representative507 - 20th Ave. E.SeattleWA
2FullerAndrew

 

Vice President, Sales
908 W. Capital WayTacomaWA
3LeverlingJanetSales Representative722 Moss Bay Blvd.KirklandWA

 

To return an employee's name and title from the database, your SELECT statement would look something like this:

SELECT FirstName,LastName,Title FROM Customers;

This would return:

FirstNameLastNameTitle
NancyDavolioSales Representative
AndrewFullerVice President, Sales
JanetLeverlingSales Representative

 

To refine the results further, you might add a WHERE clause:

SELECT FirstName,LastName FROM Customers

WHERE City='Tacoma';

This returns the FirstName and LastName of any Employee who is  from Tacoma:

FirstNameLastName
AndrewFuller

 

Note that SQL returns data in a row/column form that is similar to Microsoft Excel, making it very easy to view and work with. Other query languages might return data as a graph or chart.

The Power of Queries

A database has the potential to reveal complex trends and activities, but this power is only harnessed through the use of the query. A complex database will be comprised of multiple tables storing a myriad of data. A query allows you to filter it into a single table so you can more easily analyze it. 

Queries also can perform calculations on your data or automate data management tasks. You can also review updates to your data before committing them to the database.