What Is the Definition of a Database Query?

Use an SQL SELECT statement to find the information you're looking for

A database query extracts data from a database and formats it into a human-readable form. A query must be written in the syntax the database requires — usually a variant of Structured Query Language.

The Elements of a SQL Query

Database Planning
alexsl / Getty Images

SQL queries using Data Manipulation Language (the set of SQL statements that access or modify data, as opposed to the Data Definition Language that modifies the structure of the database itself) consist of four blocks, the first two of which are not optional.

At a minimum, a SQL query follows the following form:

select X from Y;

Here, the select keyword identifies what information you wish to display and the from keyword identifies where that data comes from and how those data sources associate with each other. Optionally, a where statement sets limiting criteria, and group by and order by statements associate values and display them in a specific sequence.

For example:

SELECT emp.ssn,emp.last_name,dept.department_name
FROM employees emp LEFT OUTER JOIN departments dept
ON emp.dept_no = dept.dept_no
WHERE emp.active_flag = 'Y'
ORDER BY 2 ASC;

This query results in a grid that shows the Social Security number, an employee last name, and the employee's department name—in that column order—taken from the employees and departments tables. The employees table governs, so it'll only show department names when there's a matching department number field in both tables (a left outer join is a method of linking tables wherein the left-sided table shows all results and only matching results from the right-sided table appear). Furthermore, the grid only shows employees whose active flag is set to Y, and the result is sorted in ascending order by the department name.

But all of this data exploration begins with the select statement.

The SQL SELECT Statement

SQL uses a SELECT statement to select, or extract, specific data.

Consider an example based on the Northwind database that frequently ships with database products as a tutorial. Here's an excerpt from the database's employees table: 

EmployeeID LastName FirstName Title Address City Region
1 Davolio Nancy Sales Representative 507 20th Ave. E. Seattle WA
2 Fuller Andrew

Vice President, Sales

908 W. Capital Way Tacoma WA
3 Leverling Janet Sales Representative 722 Moss Bay Blvd. Kirkland WA
 

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

SELECT FirstName,LastName,Title FROM Employees;

It would return:

FirstName LastName Title
Nancy Davolio Sales Representative
Andrew Fuller Vice President, Sales
Janet Leverling Sales Representative

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

SELECT FirstName,LastName FROM Employees
WHERE City='Tacoma';

It returns the FirstName and LastName of any employee who is from Tacoma:

FirstName LastName
Andrew Fuller

SQL returns data in a row-and-column form that is similar to Microsoft Excel, making it 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 consists of many tables storing a large amount of data. A query allows you to filter the data into a single table so that you can analyze it more easily.

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.

FAQ
  • How do you query an Access database?

    To create a query in Microsoft Access, go to Create > Query Wizard. Next, select a query type, such as Simple Query Wizard > OK. Select a table from the drop-down menu > choose your fields and the type of results you want > Finish.

  • What is Structured Query Language?

    Structured Query Language, or SQL, is a programming language used in data management systems and relational databases. Because it's easy to use and effective, it's been incorporated into commercial databases like MySQL, Sybase, Postgres, Oracle, and more.

  • How do you optimize an SQL query?

    To optimize an SQL query and make it as efficient as possible, use the SELECT statement to instruct the database to query only relevant information. Avoid using the SELECT DISTINCT statement, which takes a lot of processing power. Use wildcards only at the end of statements, and use the LIMIT statement to return only the specified number of records.

Was this page helpful?