SQL Basics

Chapter 2 - Retrieving Data

Syntax

SELECT	<the column you want to display>
FROM	<the table containing the data>

  • SELECT is a keyword (a reserved word)
  • Returns all rows from a table
  • The result is unsorted
  • The result is not filtered
  • SQL keywords are NOT case-sensitive
  • Extra spaces are ignored

Select the product names from the Products table

SELECT	prod_name
FROM	Products;
prod_name
Fish bean bag toy
Bird bean bag toy
Rabbit bean bag toy
8 inch teddy bear
12 inch teddy bear
18 inch teddy bear
Raggedy Ann
King doll
Queen doll
  • To select multiple columns, separate each column name in the SELECT statement with a comma

Select the product ID, name and price from the Products table

SELECT	prod_id,
	prod_name,
	prod_price
FROM	Products;
prod_idprod_nameprod_price
BNBG01Fish bean bag toy3.49
BNBG02Bird bean bag toy3.49
BNBG03Rabbit bean bag toy3.49
BR018 inch teddy bear5.99
BR0212 inch teddy bear8.99
BR0318 inch teddy bear11.99
RGAN01Raggedy Ann4.99
RYL01King doll9.49
RYL02Queen doll9.49
  • To select all columns, use an asterisk (*) in the SELECT statement

Select all columns from the Products table

SELECT	*
FROM	Products;
prod_idvend_idprod_nameprod_priceprod_desc
BNBG01DLL01Fish bean bag toy3.49Fish bean bag toy complete with bean bag worms with which to feed it
BNBG02DLL01Bird bean bag toy3.49Bird bean bag toy eggs are not included
BNBG03DLL01Rabbit bean bag toy3.49Rabbit bean bag toy comes with bean bag carrots
BR01BRS018 inch teddy bear5.998 inch teddy bear comes with cap and jacket
BR02BRS0112 inch teddy bear8.9912 inch teddy bear comes with cap and jacket
BR03BRS0118 inch teddy bear11.9918 inch teddy bear comes with cap and jacket
RGAN01DLL01Raggedy Ann4.9918 inch Raggedy Ann doll
RYL01FNG01King doll9.4912 inch king doll with royal garments and crown
RYL02FNG01Queen doll9.4912 inch queen doll with royal garments and crown

If you do not need every row in a table, limit the number of rows returned

  • SQL Server / MS Access: put the TOP keyword followed by the number of rows in the SELECT statement
SELECT TOP 5 cust_name
FROM Customers;
  • MySQL / MariaDB / PostgreSQL / SQLite: put the LIMIT keyword followed by the number of rows after the FROM clause
SELECT cust_name
FROM Customers
LIMIT 5;
  • Oracle: use a WHERE clause with ROWNUM to limit the number of rows
SELECT cust_name
FROM Customers
WHERE rownum <= 5;
  • DB2 / Oracle: put FETCH FIRST X ROWS ONLY after the FROM clause
SELECT cust_name
FROM Customers
FETCH FIRST 5 ROWS ONLY;

For more variations, see W3Schools


Select the first 5 rows from the Products table

SELECT prod_name
FROM   Products
LIMIT  5;
prod_name
Fish bean bag toy
Bird bean bag toy
Rabbit bean bag toy
8 inch teddy bear
12 inch teddy bear
  • Use DISTINCT in the SELECT statement to return unique rows
  • DISTINCT applies to all columns in the SELECT statement

Select unique vend_id values from the Products table

SELECT	DISTINCT vend_id 
FROM	Products;
vend_id
BRS01
DLL01
FNG01
  • A single-line comment: -- comment
-- This query is straightforward
SELECT * -- All columns are selected here
FROM Customers;
-- LIMIT 1;
  • A multi-line comment: /* comment block */
/* Do not remove this query yet
SELECT * 
FROM Customers;
LIMIT 1; */
  1. Select everything (all rows and columns) from the OrderItems table (18)


  1. Select prod_name and prod_price from the Products table (9)


  1. Select order_num from the Orders table (5)


  1. Select unique order_num values from the OrderItems table (5)


  1. Select all columns from the first 5 rows of the OrderItems table (5)