Lesson 5 - SQL SELECT
Lesson 5 - SQL SELECT
LESSON 5
SQL SELECT
To select all columns from a database table, we use the * character. For
example,
-- select all columns from Customers table
SELECT * FROM Customers;
Here, the SQL command selects all columns of the Customers table.
SQL SELECT ALL
SQL SELECT WHERE CLAUSE
-- select age and country columns from customers table where the country is 'USA'
SELECT age, country
FROM Customers
WHERE country = 'USA';
Here, the SQL command selects the age and country columns of all the
customers whose country is USA.
We can also use the WHERE clause with the UPDATE statement to edit
existing rows in a database table.
SQL SELECT WHERE CLAUSE
SQL OPERATORS
The above SQL command selects all the customers from the Customers table
having first_name John.
SQL OPERATORS
The above SQL command selects all the customers from the Customers table
whose age is greater than 25.
SQL OPERATORS
The SQL AND operator selects data if all conditions are TRUE.
For example,
-- select the first_name and last_name of all customers -- who live in 'USA' and have the last
name 'Doe' SELECT first_name, last_name
FROM Customers
WHERE country = 'USA’
AND last_name = 'Doe';
Here, the SQL command selects first_name and of last_name all customers
whose country is USA and last_name is Doe from the Customers table.
SQL AND OPERATOR
SQL OR OPERATOR
--select
For example,
first and last name of customers -- who either live in the USA -- or have the last name
'Doe'
SELECT first_name, last_name
FROM Customers
WHERE country = 'USA' OR last_name = 'Doe';
Here, the SQL command selects first_name and last_name of all customers
where the country is USA or if their last name is Doe from the Customers
table.
SQL OR OPERATOR
SQL NOT OPERATOR
The SQL NOT operator selects data if the given condition is FALSE.
For example,
-- select customers who don't live in the USA
SELECT first_name, last_name
FROM Customers
WHERE NOT country = 'USA';
Here, the SQL command selects first_name and last_name of all customers
where the country is not USA from the Customers table.
SQL NOT OPERATOR
COMBINING MULTIPLE OPERATORS
Example
-- exclude customers who are from the USA and have 'Doe' as their last name
SELECT *
FROM customers
WHERE NOT country = 'USA' AND NOT last_name = 'Doe';
Here, the SQL command selects all customers where the country is
not USA and last_name is not Doe from the Customers table.
COMBINING MULTIPLE OPERATORS
SQL SELECT DISTINCT
SQL SELECT DISTINCT
Here, the SQL command selects only the unique values of age from the
Customers table.
SYNTAX OF SQL SELECT DISTINCT
The SQL command selects unique countries from the Customers table.
EXAMPLE: SYNTAX OF SQL SELECT DISTINCT
SQL DISTINCT ON MULTIPLE COLUMNS
We can use SQL DISTINCT with the COUNT() function to count the number
of unique rows.
Let's look at an example.
-- count the unique countries where customers are from
SELECT COUNT(DISTINCT country)
FROM Customers;
The AS keyword is used to give columns or tables a temporary name that can
be used to identify that column or table later.
Example
SELECT first_name AS name
FROM Customers;
The SQL command selects the first_name column from the Customers table.
However, the column name is changed to name in the result set.
SQL AS ALIAS SYNTAX
Example:
The SQL command selects first_name and last_name. And, the name of the column will be
full_name in the result set.
SQL AS WITH EXPRESSION
-- concatenate first_name, empty space, and last_name -- into a single column named
full_name in the result set
SELECT first_name || ' ' || last_name AS full_name
FROM Customers;
The SQL command will concatenate the first_name and last_name columns in the result set
as full_name.
Notice that we have also concatenated an empty space ' ' between first_name and last_name.
This ensures that the data from these columns are separated by a space in the result set.
SQL AS WITH EXPRESSION
SQL SELECT LIMIT, TOP, FETCH FIRST
SQL SELECT LIMIT, TOP, FETCH FIRST
The SQL LIMIT keyword allows us to specify the number of records in the result set.
SELECT first_name, age
Example
FROM Customers
LIMIT 2;
The SQL command selects the first 2 rows from the table.
SQL LIMIT WITH OFFSET CLAUSE
The OFFSET keyword is used with LIMIT to specify the starting rows from where to select
-- LIMIT 2 selects two results -- OFFSET 3 excludes the first three results
the data.
SELECT For example,
first_name, last_name
FROM Customers
LIMIT 2 OFFSET 3;
The SQL command selects 2 rows starting from the fourth row. OFFSET 3 means the first 3 rows
are excluded.
SQL LIMIT WITH OFFSET CLAUSE
SQL LIMIT WITH OFFSET CLAUSE
MySQL, PostgreSQL,
LIMIT
SQLite
The TOP keyword is used in place of LIMIT with the following database
systems:
SQL Server
MS Access
SELECT TOP 2 first_name, last_name
FROM
Let's look at an example.
Customers;
The SQL command selects first_name and last_name of the first 2 rows.
SQL TOP CLAUSE
The SQL command selects the first 2 rows from the table.
SQL TOP CLAUSE
SQL FETCH FIRST CLAUSE
The FETCH FIRST n ROWS ONLY clause is used with the Oracle database
system.
Let's look at an example.
SELECT *
FROM Customers
FETCH FIRST 2 ROWS ONLY;
The SQL command selects the first 2 rows from the table.
SQL IN AND NOT IN OPERATORS
We use the IN operator with the WHERE clause to match values in a list.
Example
-- select customers from the USA
SELECT first_name, country
FROM Customers
WHERE country IN ('USA');
The SQL command selects rows from the Customers table
whose country value is 'USA'.
SQL IN SYNTAX
SELECT column1, column2, ...
FROM table
WHERE column IN (value1, value2, ...);
The SQL command selects rows if the country is either the USA or the UK.
EXAMPLE: SQL IN
EXAMPLE: IN OPERATOR TO SELECT ROWS BASED ON
COUNTRY VALUE
The IN operator can be used to choose rows where a specific value is present in the
-- select rows with value 'USA' in the country column
specified field.
SELECT first_name, country
FROM Customers
WHERE 'USA' IN (country);
The SQL command selects the rows if the USA value exists in the country field.
EXAMPLE: IN OPERATOR TO SELECT ROWS BASED ON
COUNTRY VALUE
SQL NOT IN OPERATOR
The NOT IN operator excludes the rows that match values in the list. It
returns
-- select rowsall thecountry
where rows except
is not in the
UK orexcluded
UAE rows.
SELECT first_name, country
FROM Customers
WHERE country NOT IN ('UK', 'UAE');
In SQL, the BETWEEN operator with the WHERE clause selects values within a
given range.
Example
-- select rows where the amount is between 200 and 600
SELECT *
FROM Orders
WHERE amount BETWEEN 200 AND 600;
Here, the SQL command selects all columns from the Orders table where the
amount is between 200 and 600 (including 200 and 600).
SQL BETWEEN SYNTAX
SELECT column1, column2, ...
FROM table
WHERE column BETWEEN value1 AND value2;
column1, column2, ... are the columns you want to filter
table is the name of the table
column is the name of the column where we want to specify a range of values
BETWEEN is an operator used to specify a range of values for the column
value1 and value2 are the lower and upper bounds of the range
EXAMPLE: SQL BETWEEN
We selected item and amount columns from Orders that have amounts
between 300 and 500 (including 300 and 500).
EXAMPLE: SQL BETWEEN
SQL NOT BETWEEN OPERATOR
The NOT BETWEEN operator is used to exclude the rows that match the values in
the range. It returns all the rows except the excluded rows.
Let's look at an example.
-- exclude rows with amount between 300 and 500
Here, we selected all the items from Orders except the rows that have amounts
between 300 and 500 (300 and 500 are also excluded).
SQL NOT BETWEEN OPERATOR
SQL BETWEEN OPERATOR WITH TEXT
-- select rows where items begin with letters between 'I' and 'L' inclusive -- this includes all
items starting with 'I', 'J', 'K', and any items starting with 'L'
SELECT item, amount
FROM Orders WHERE item BETWEEN 'I' AND 'L';
The SQL command selects all orders where the item names begin with letters
between I and L.
SQL BETWEEN OPERATOR WITH TEXT
SQL BETWEEN OPERATOR WITH TEXT
Generally, the words starting with the endpoint (L) are not selected.
If you need to include all the words that start with L as well, we can use ~.
-- select rows where items begin with letters between 'I' and 'L' -- include all items beginning
with 'L' followed by other characters
SELECT item, amount
FROM Orders WHERE item BETWEEN 'I' AND 'L~';
SQL BETWEEN DATES
In SQL, we can also use BETWEEN to filter data between two dates.
Let's look at an example.
SELECT *
FROM Teams WHERE registered BETWEEN '2021-01-01' AND '2022-11-01';
Here, we selected the teams who registered between 2021-01-01 and 2022-
11-01.
SQL IS NULL AND IS NOT NULL
IS NULL SYNTAX
SELECT *
FROM Employee WHERE email IS NULL;
The above SQL query retrieves all the rows from the Employee table
where the value of the email column is NULL .
IS NULL SYNTAX
IS NOT NULL
In SQL, the IS NOT NULL condition is used to select rows if the specified field is NOT NULL. It has the
following syntax:
column_name is the name of the column you want to check for NOT NULL
IS NOT NULL
NULL.
IS NOT NULL
IS NULL WITH COUNT()
We can use the COUNT() function with IS NULL to count the
number of rows with an empty field. For example,
SELECT COUNT(*)
FROM Employee
WHERE email IS NULL;
Here, the SQL query retrieves the count of all the rows
from the Employee table where the value of the email column
is NULL.
IS NULL WITH COUNT()
SQL MAX() AND MIN()
SQL MAX() AND MIN()
In SQL,
•The MAX() function returns the maximum value of a column.
•The MIN() function returns the minimum value of a column.
SQL MAX() FUNCTION
SELECT MAX(columnn)
Here,
FROM table;
column is the name of the column you want to filter
table is the name of the table to fetch the data from
SQL MAX() FUNCTION
For example,
SELECT MAX(age)
FROM Customers;
The SQL command returns the largest value from the age column.
SQL MAX() FUNCTION
ALIASES WITH MAX() AND MIN()
In the above examples, the field names in the result sets were MIN(age) and
MAX(age).
It is also possible to give custom names to these fields using the AS keyword.
For example,
-- use max_age as an alias for the maximum age
The field name MAX(age) is replaced with max_age in the result set.
ALIASES WITH MAX() AND MIN()
MAX() AND MIN() WITH STRINGS
The MAX() and MIN() functions also work with texts. For example,
As we know, the MAX() function returns the maximum value. Similarly, the MIN() function
returns the minimum value.
However, if we want to select the whole row containing that value, we can use the nested
SELECT statement like this.
-- MIN() function in a nested SELECT statement
SELECT *
FROM Customers
WHERE age = ( SELECT MIN(age)
FROM Customers );
The SQL command selects all the customers with the smallest age.
MAX() AND MIN() IN NESTED SELECT
SQL COUNT()
MAX() AND MIN() IN NESTED SELECT
The SQL COUNT() function returns the number of records returned by a query.
Example
-- returns the count of rows in the Orders table
SELECT COUNT(*)
FROM Orders;
Here, the above SQL command returns the count of rows in the Orders table.
COUNT() SYNTAX
SELECT COUNT(*)
FROM table;
SELECT COUNT(*)
FROM Customers;
The above SQL command counts and returns the number of rows in the Customers table.
COUNT() SYNTAX
EXAMPLE: SPECIFY COLUMN TO COUNT
We can also specify a column name in COUNT() to only count the rows in that particular
column.
The above SQL query returns the count of non-null values in the age column.
EXAMPLE: SPECIFY COLUMN TO COUNT
COUNT() WITH WHERE
We can use COUNT() with WHERE to count rows that match the given value.
SELECT COUNT(country)
FROM Customers
WHERE country = 'UK';
The SQL command returns the count of customers whose country is the UK.
COUNT() WITH WHERE
COUNT() WITH DISTINCT
If we need to count the number of unique rows, we can use the COUNT() function with the
DISTINCT clause.
For example,
The SQL SUM() function is used to calculate the cumulative sum of numeric values in a
column.
It has the following syntax:
SELECT SUM(column_name)
FROM table;
SUM is the function that returns the cumulative sum of numeric values
column_name is the column to which we apply the SUM function
table is the name of the table to fetch the data from
SQL SUM() FUNCTION
SELECT SUM(amount)
AS total_sales
FROM Orders;
The SQL command returns the sum of amount of all orders.
SQL SUM() FUNCTION
EXAMPLE 1: SQL SUM() FUNCTION
--select the sum of the amount of id 4 from orders
The SQL command returns the total amount to be paid by the customer having id 4.
EXAMPLE 1: SQL SUM() FUNCTION
SQL AVG() FUNCTION
The SQL AVG() function is used to calculate the average of numeric values in a
column. It has the following syntax:
SELECT AVG(column_name)
FROM table;
--selects the average amount spent by each customer from the Orders table