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

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_nameFROM employees emp LEFT OUTER JOIN departments dept ON emp.dept_no = dept.dept_noWHERE 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: 

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:

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

SELECT FirstName,LastName FROM EmployeesWHERE City=‘Tacoma’;

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

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.

  • 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.

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.

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.

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.

Get the Latest Tech News Delivered Every Day

  • Databases for Beginners

  • How to Export Data to Excel

  • What Is a DDL File?

  • What Is mysqldump and How Do I Use It?

  • How to Use the Excel INDEX Function

  • What is MySQL?

  • What Is an ADP File?

  • Glossary of Common Database Terms

  • The Basics of Database Normalization

  • Putting a Database in Third Normal Form (3NF)

  • What Is a DBF File?

  • Data Control Language (DCL)

  • DNS Servers: What Are They and Why Are They Used?

  • Creating Simple Queries in Access 2010

  • Microsoft Access GROUP BY Query

  • How to Create an Excel Lookup Formula With Multiple Criteria

  • Facebook

  • Twitter

Hit Refresh on Your Tech News

  • About Us

  • Privacy Policy

  • Editorial Guidelines

  • Terms of Use

  • Careers

  • Advertise

  • Contact

  • EU Privacy

  • NEWS

  • HOW TO

  • FEATURES

  • ABOUT US