SELECT Statement
This SQL tutorial explains how to use the SQL SELECT statement with syntax, examples, and practice exercises.
Description
The SQL SELECT statement is used to retrieve records from one or more tables in your SQL database.
Syntax
The syntax for the SQL SELECT statement is:
SELECT expressions FROM tables [WHERE conditions];
Parameters or Arguments
- expressions
- The columns or calculations that you wish to retrieve.
- tables
- The tables that you wish to retrieve records from. There must be at least one table listed in the FROM clause.
- WHERE conditions
- Optional. The conditions that must be met for the records to be selected. If no conditions are provided, then all records will be selected.
Example - Select all fields from one table
Let's look at an example showing how to use the SQL SELECT statement to select all fields from a table.
SELECT * FROM suppliers WHERE city = 'Newark' ORDER BY city DESC;
In this SQL SELECT statement example, we've used * to signify that we wish to view all fields from the suppliers table where the supplier resides in Newark. The result set is sorted by city in descending order.
Example - Select individual fields from one table
You can also use the SQL SELECT statement to select individual fields from the table, as opposed to all fields from the table.
For example:
SELECT supplier_name, city, state FROM suppliers WHERE supplier_id > 1000 ORDER BY name ASC, city DESC;
This SQL SELECT example would return only the supplier_name, city, and state fields from the suppliers table where the supplier_id value is greater than 1000. The results are sorted by supplier_name in ascending order and then city in descending order.
Example - Select fields from multiple tables
You can also use the SQL SELECT statement to retrieve fields from multiple tables.
SELECT orders.order_id, suppliers.name FROM suppliers INNER JOIN orders ON suppliers.supplier_id = orders.supplier_id ORDER BY order_id;
This SQL SELECT example joins two tables together to gives us a result set that displays the order_id and supplier name fields where the supplier_id value existed in both the suppliers and orders table. The results are sorted by order_id in ascending order.
Learn more about SQL joins.
Practice Exercise #1:
Based on the employees table below, select all fields from the employees table whose salary is less than or equal to $52,500 (no sorting is required):
CREATE TABLE employees ( employee_number int NOT NULL, employee_name char(50) NOT NULL, salary int, CONSTRAINT employees_pk PRIMARY KEY (employee_number) );
Solution for Practice Exercise #1:
The following SQL SELECT statement would select these records from the employees table:
SELECT * FROM employees WHERE salary <= 52500;
Practice Exercise #2:
Based on the suppliers table below, select the unique city values that reside in the state of Florida and order the results in descending order by city:
CREATE TABLE suppliers ( supplier_id int NOT NULL, supplier_name char(50) NOT NULL, city char(50), state char(25), CONSTRAINT suppliers_pk PRIMARY KEY (supplier_id) );
Solution for Practice Exercise #2:
The following SQL SELECT statement would select these records from the suppliers table:
SELECT DISTINCT city FROM suppliers WHERE state = 'Florida' ORDER BY city DESC;
Practice Exercise #3:
Based on the suppliers table and the orders table below, select the supplier_id and supplier_name from the suppliers table and select the order_date from the orders table where there is a matching supplier_id value in both the suppliers and orders tables. Order the results by supplier_id in descending order.
CREATE TABLE suppliers ( supplier_id int NOT NULL, supplier_name char(50) NOT NULL, city char(50), state char(25), CONSTRAINT suppliers_pk PRIMARY KEY (supplier_id) ); CREATE TABLE orders ( order_id int NOT NULL, supplier_id int NOT NULL, order_date date NOT NULL, quantity int, CONSTRAINT orders_pk PRIMARY KEY (order_id) );
Solution for Practice Exercise #3:
The following SQL SELECT statement would select these records from the suppliers and orders table (using a SQL INNER JOIN):
SELECT suppliers.supplier_id, suppliers.supplier_name, orders.order_date FROM suppliers INNER JOIN orders ON suppliers.supplier_id = orders.supplier_id ORDER BY supplier_id DESC;
Practice Exercise #4:
Based on the customers and old_customers table, select the customer_id and customer_name from the customers table that exist in the old_customers table (matching the customer_id field from the customers table to the old_customer_id field in the old_customers table). Order the results in ascending order by customer_name and then descending order by customer_id.
CREATE TABLE customers ( customer_id int NOT NULL, customer_name char(50) NOT NULL, city char(50), CONSTRAINT customers_pk PRIMARY KEY (customer_id) ); CREATE TABLE old_customers ( old_customer_id int NOT NULL, old_customer_name char(50) NOT NULL, old_city char(50), status char(20), CONSTRAINT old_customers_pk PRIMARY KEY (old_customer_id) );
Solution for Practice Exercise #4:
The following SQL SELECT statement would select the records from the customers table (using the SQL EXISTS clause):
SELECT customer_id, customer_name FROM customers WHERE EXISTS ( SELECT old_customers.old_customer_id FROM old_customers WHERE old_customers.old_customer_id = customers.customer_id ) ORDER BY customer_name ASC, customer_id DESC;
Or alternatively you could exclude the ASC keyword for customer_name in the ORDER BY clause. Both of these SELECT statements would generate the same results:
SELECT customer_id, customer_name FROM customers WHERE EXISTS ( SELECT old_customers.old_customer_id FROM old_customers WHERE old_customers.old_customer_id = customers.customer_id ) ORDER BY customer_name, customer_jd DESC;