Science, Tech, Math › Computer Science Retrieving Data with SQL Queries: Introducing the SELECT Statement Share Flipboard Email Print Science, Tech, Math PHP Programming Perl Python Java Programming Javascript Programming Delphi Programming C & C++ Programming Ruby Programming Visual Basic View More By Mike Chapple Mike Chapple Writer University of Idaho Auburn University Notre Dame Mike Chapple is an IT professional with more than 10 years' experience cybersecurity and extensive knowledge of SQL and database management. Learn about our Editorial Process Updated on March 09, 2021 Reviewed by Ryan Perian Reviewed by Ryan Perian Western Governors University Ryan Perian is a certified IT specialist who holds numerous IT certifications and has 12+ years' experience working in the IT industry support and management positions. Learn about our Editorial Process 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. Getty Images/ermingut The General Form of the SELECT Statement The general form of the SELECT statement appears below: SELECT select_listFROM sourceWHERE condition(s)GROUP BY expressionHAVING conditionORDER 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 Cite this Article 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 29, 2023). copy citation By clicking “Accept All Cookies”, you agree to the storing of cookies on your device to enhance site navigation, analyze site usage, and assist in our marketing efforts. Cookies Settings Accept All Cookies