Software & Apps Apps 105 105 people found this article helpful What Is the Definition of a Database Query? Use an SQL SELECT statement to find the information you're looking for By Mike Chapple Writer Former Lifewire writer Mike Chapple is an IT professional with more than 10 years' experience cybersecurity and extensive knowledge of SQL and database management. our editorial process Twitter Mike Chapple Updated November 18, 2019 Apps Best Apps Tweet Share Email 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 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_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: 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 EmployeesWHERE 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/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. More from Lifewire Retrieving Data with SQL Queries: Introducing the SELECT Statement How to Count Database Table Values With SQL COUNT Structured Query Language Frequently Asked Questions Creating Databases and Tables in SQL Creating Simple Queries in Access 2010 Basic Keys That Make Database Management Easy Creating Reports With Microsoft Access 2010 What You Need to Know About Structured Query Language A Guide to Using Inner Joins in SQL to Group Data From Multiple Tables Pattern Matching in SQL Queries An Overview of NoSQL Databases Controlling Data Access With Views in SQL Building an Access Database in Office 365 Spreadsheets vs. Databases Retrieving Data From Multiple Tables With SQL Inner Joins What Is a Database Schema?