Retrieving Data with SQL Queries: Introducing the SELECT Statement

The Structured Query Language offers database users a powerful and flexible data retrieval mechanism — the SELECT statement. In this article, we'll take a look at the general form of the SELECT statement and compose a few sample database queries together. If this is your first foray into the world of the Structured Query Language, you may wish to review SQL fundamentals before continuing. If you're looking to design a new database from scratch, learning how to create databases and tables in SQL should prove a good jumping-off point.

Now that you've brushed up on the basics, let's begin our exploration of the SELECT statement. As with previous SQL lessons, we'll continue to use statements that are compliant with the ANSI SQL standard. You may wish to consult the documentation for your DBMS to determine whether it supports advanced options that may enhance the efficiency and/or efficacy of your SQL code.  

Programming language
Getty Images/ermingut

The General Form of the SELECT Statement

The general form of the SELECT statement appears below:

SELECT select_list
FROM source
WHERE condition(s)
GROUP BY expression
HAVING condition
ORDER BY expression

The first line of the statement tells the SQL processor that this command is a SELECT statement and that we wish to retrieve information from a database. The select_list allows us to specify the type of information we wish to retrieve. The FROM clause in the second line specifies the specific database table(s) involved and the WHERE clause gives us the capability to limit the results to those records that meet the specified condition(s). The final three clauses represent advanced features outside the scope of this article – we'll explore them in future SQL articles.

The easiest way to learn SQL is by example. With that in mind, let's begin looking at some database queries. Throughout this article, we'll use the employee's table from the fictional XYZ Corporation human resources database to illustrate all of our queries. Here's the entire table:

EmployeeID

LastName

FirstName

Salary

ReportsTo

1

Smith

John

32000

2

2

Scampi

Sue

45000

NULL

3

Kendall

Tom

29500

2

4 Jones Abraham 35000 2
5 Allen Bill 17250 4
6 Reynolds Allison 19500 4
7 Johnson Katie 21000 3

Retrieving an Entire Table

XYZ Corporation's Director of Human Resources receives a monthly report providing salary and reporting information for each company employee. The generation of this report is an example of the SELECT statement's simplest form. It simply retrieves all of the information contained within a database table — every column and every row. Here's the query that will accomplish this result:

SELECT *
FROM employees

Pretty straightforward, right? The asterisk (*) appearing in the select_list is a wildcard used to inform the database that we would like to retrieve information from all of the columns in the employee's table identified in the FROM clause. We wanted to retrieve all of the information in the database, so it wasn't necessary to use a WHERE clause to restrict the rows selected from the table. Here's what our query results look like:

EmployeeID LastName FirstName Salary ReportsTo
---------- -------- --------- ------ ---------
1 Smith John 32000 2
2 Scampi Sue 45000 NULL
3 Kendall Tom 29500 2
4 Jones Abraham 35000 2
5 Allen Bill 17250 4
6 Reynolds Allison 19500 4
7 Johnson Katie 21000 3
Format
mla apa chicago
Your Citation
Chapple, Mike. "Retrieving Data with SQL Queries: Introducing the SELECT Statement." ThoughtCo, Nov. 18, 2021, thoughtco.com/introducing-the-select-statement-4091916. Chapple, Mike. (2021, November 18). Retrieving Data with SQL Queries: Introducing the SELECT Statement. Retrieved from https://www.thoughtco.com/introducing-the-select-statement-4091916 Chapple, Mike. "Retrieving Data with SQL Queries: Introducing the SELECT Statement." ThoughtCo. https://www.thoughtco.com/introducing-the-select-statement-4091916 (accessed March 28, 2024).