Structured Query Language (SQL) is the standard programming language used to communicate with relational databases. Because data usage in business is growing at a staggering rate, the demand for people who understand SQL, relational databases, and data management is rising.

The American National Standards Institute establishes SQL as a standard for relational database management, so programmers and administrators can learn just one single language that works with only minor adjustments across a wide variety of database platforms, applications, and products. If you’re looking to build a career in programming or database management or if you’d like to better understand the workings of your website or company databases, SQL is an important language to learn.

Binary, Hands, Keyboard, Tap, Enter, Input, Store, One

SQL is a language with a syntax all its own, consisting of statements, clauses, and other pieces of code such as operators that establish parameters for limiting the query. An SQL statement is a complete piece of code that goes to a database management system, or DBMS, in order to perform a variety of functions on data contained in the database. You can request specific information from the database by writing a query, which is a statement that returns or delivers the information you want from the tables within the database. The SELECT statement is the most common operation in SQL, as it specifies which data to return from the database.

The Complete SQL Bootcamp: Go from Zero to Hero

Last Updated May 2022

Bestseller
  • 83 lectures
  • All Levels
4.7 (186,181)

Become an expert at SQL! | By Jose Portilla

Explore Course

The SELECT Statement and Its Significance

SELECT statements are frequently called Data Query Language (DQL) in SQL. In relational databases, you store data in table form — under columns and rows defined by the user — and those tables populate with the INSERT statement. The SELECT command operates with the FROM clause to retrieve or extract information from database tables and present it in an organized and readable way.

The SELECT keyword in the query states which rows and columns you want to display as the result set of a query. Using the SELECT statement, you can specify the values that match the rows in the table that you want the query to return.

In its simplest form, the SELECT statement must contain the following elements: a SELECT clause that specifies the columns containing the values matching the query and a FROM clause that specifies the TABLE containing the columns listed in the SELECT clause.

The SELECT statement can also have many optional clauses that refine the query and return precise results. Commonly used clauses include:

The basic syntax for a SELECT statement looks like this:

SELECT [column 1, column 2, …] FROM source_table;

To display all the columns from a table in the result set, use the symbol “*” after SELECT. Likewise, you can set other flags such as the option to return only unique rows (with the ONLY UNIQUE) modifier.

Combining SQL SELECT and INSERT Statements

An INSERT statement containing a nested SELECT statement allows you to quickly populate a table with one or more rows from the result set of the SELECT command. You can insert multiple records in this way, like this:

INSERT INTO table (column1, column2, … ) SELECT expression1, expression2, … FROM source_tables WHERE conditions;

The syntax of this statement contains several parameters:

That means “column1” will have the value of “expression1,” “column2” will have the value of “expression2,” and so on. The ellipsis, or triple dots, after each of these elements indicates that the operation will apply to other columns and expressions in the series:


When performing insertions, you can also use common keywords such as auto increment (which creates an auto incrementing key).

The SQL SELECT INTO Statement

The SELECT INTO statement selects data from one table and inserts it into a new table. You can choose one or more columns, or all columns, to copy into the new table. If you want to copy all columns, use the “*” after the SELECT statement. The syntax for this statement looks like this:

SELECT * INTO newtable [IN externaldb] FROM table1

The syntax in this statement includes four parameters. 

Here’s an example of an SQL SELECT INTO statement that creates a copy of an entire table of Vendors in the SQL server:

SELECT * INTO Vendorcopy2014 FROM Vendors;

But if we want to copy only specific columns into the new table, we’ll enter:

SELECT [column 1, column 3, ….] INTO newtable FROM table1;

This statement will generate a completely new table with the column names and types defined in the SELECT operator. For example, suppose we want to copy only the columns “Vendor Name” and “Country Name” into the new table. That statement looks like this:

SELECT VendorName, CountryName INTO Vendorcopy2014 FROM Vendors;

The SQL SELECT DISTINCT Statement

The DISTINCT clause is used in a SELECT statement to remove duplicates from the result set of the query. Here is the syntax for this statement:

SELECT DISTINCT expressions FROM tables WHERE conditions;

This statement includes these parameters:

If you don’t want to remove duplicate data, or if you’re sure that there will be no duplicates in the result set, then you can use ALL instead of DISTINCT. But ALL is the default keyword in SQL statements, so we usually omit it from query statements.

Conclusion

This tutorial provides an overview of the SQL SELECT statement and related clauses, but there’s so much more to explore. Take a look at our blog that highlights the most useful SQL commands to learn how to write basic SQL queries and other code statements. As you develop the knowledge and skills to become a database administrator and development, you’ll need to begin writing customized SQL code in order to meet the requirements of users as efficiently as possible.

If you want another in-depth look at another one of SQL’s most important operators, let’s progress to the INSERT INTO command.

Page Last Updated: May 2022