0% found this document useful (0 votes)
10 views

Lesson 5 - SQL SELECT

Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Lesson 5 - SQL SELECT

Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 113

SQL SELECT

LESSON 5
SQL SELECT

 The SQL SELECT statement is used to select (retrieve)


data from a database table.
SQL SELECT SYNTAX

 The syntax of the SQL SELECT statement is:


SELECT column1, column2, ...
FROM table;
 Here,
•column1, column2, ... are the table columns
•table is the table name from where we select the data
SQL SELECT SYNTAX
SQL SELECT ALL

 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

 A SELECT statement can have an optional WHERE clause. The WHERE


clause allows us to fetch records from a database table that matches
specified condition(s). For example,
-- select all columns from the customers table with last_name 'Doe'
SELECT * FROM Customers
WHERE last_name = 'Doe';
 Here, the SQL command selects all customers from the Customers table with
the last_name Doe.
SQL SELECT WHERE CLAUSE
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

 1. Equal to Operator (=)


-- select all columns from Customers table with first name 'John'
SELECT *
FROM Customers
WHERE first_name = 'John';

 The above SQL command selects all the customers from the Customers table
having first_name John.
SQL OPERATORS

 2. Greater than (>)


-- select all columns from Customers table with age greater than 25
SELECT *
FROM Customers
WHERE age > 25;

 The above SQL command selects all the customers from the Customers table
whose age is greater than 25.
SQL OPERATORS

 3. AND Operator (AND)


-- select all columns from Customers table with last_name 'Doe' and country 'USA'
SELECT *
FROM Customers
WHERE last_name = 'Doe’
AND country = 'USA';

 The above SQL command selects all the customers from


the Customers table having last_name Doe and country USA.
SQL AND, OR, AND NOT OPERATORS
THE AND, OR, AND NOT OPERATORS IN SQL ARE USED WITH THE WHERE OR HAVING
CLAUSES.
SQL AND OPERATOR

 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

 The SQL OR operator selects data if any one condition is TRUE.

--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

 It is also possible to combine multiple AND, OR and NOT operators in an SQL


statement.
 For example, let's suppose we want to select customers where country is
either USA or UK, and age is less than 26.
-- select customers who live in either USA or UK and whose age is less than 26
SELECT *
FROM Customers
WHERE (country = 'USA' OR country = 'UK') AND age < 26;
COMBINING MULTIPLE OPERATORS
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

 The SELECT DISTINCT statement retrieves distinct values from a database


table.
 Example
-- select the unique ages from the Customers table
SELECT DISTINCT age
FROM Customers;

 Here, the SQL command selects only the unique values of age from the
Customers table.
SYNTAX OF SQL SELECT DISTINCT

SELECT DISTINCT column1, column2 ...


FROM table;

 column1, column2, ... are the table columns


 table is table name from where we retrieve the distinct columns
EXAMPLE: SYNTAX OF SQL SELECT DISTINCT

-- select the unique countries from the customers table


SELECT DISTINCT country
FROM Customers;

 The SQL command selects unique countries from the Customers table.
EXAMPLE: SYNTAX OF SQL SELECT DISTINCT
SQL DISTINCT ON MULTIPLE COLUMNS

 We can also use SELECT DISTINCT with multiple columns.


 For example,
-- select rows if the first name and country of a customer is unique
SELECT DISTINCT country, first_name
FROM Customers;

 The command selects rows if combinations of country and first_name are


unique. Meaning, the result will include each pair of country and first_name
only once.
SQL DISTINCT ON MULTIPLE COLUMNS
DISTINCT WITH COUNT

 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 SQL command returns the count of unique countries.


DISTINCT WITH COUNT
SQL SELECT AS ALIAS
SQL SELECT AS ALIAS

 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

 The syntax of the SQL AS command is:


SELECT column_1 AS alias_1,
column_2 AS alias_2,
... ...column_n AS alias_n
FROM table_name;
 column_1, column_2,...column_n are the table columns
 alias_1, alias_2,...alias_n are the aliases of the table columns
SQL AS ALIAS SYNTAX

 Example:

SELECT first_name AS name


FROM Custome

 The SQL command selects the first_name column of Customers.


However, the column name will change to name in the result set.
SQL AS ALIAS SYNTAX
SQL AS WITH MORE THAN ONE COLUMN

 We can also use aliases with more than one column.


SELECT customer_id AS cid, first_name AS name
 ForCustomers;
FROM example,

 The SQL command selects customer_id as cid and first_name as name.


SQL AS WITH EXPRESSION

SELECT CONCAT(first_name, ' ', last_name) AS full_name


 We can combine data from multiple columns and represent it in a single column using
FROM Customers;
the CONCAT() function. For 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

 Note: The clause is not supported in all Database Management


LIMIT

Systems (DBMS). Different DBMS use different keywords to select a


fixed number of rows. For example,
Keyword Database System

TOP SQL Server, MS Access

MySQL, PostgreSQL,
LIMIT
SQLite

Oracle, PostgreSQL (as


FETCH FIRST part of SQL:2008
standard), DB2
SQL TOP CLAUSE

 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

 We can also use * with TOP to select all columns.


SELECT TOP 2 *
FROM Customers;

 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, ...);

 column1, column2, ... are the table columns.


 table is the table name from where we select the data.
 column is where the values are compared against.
 IN operator specifies values that the column value should be compared
against.
 value1, value2, ... are the values the column value is compared against.
EXAMPLE: SQL IN

-- select rows if the country is either USA or UK

SELECT first_name, country


FROM Customers
WHERE country IN ('USA', 'UK');

 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');

 The SQL command selects rows if the UK or UAE is not in


the country column.
SQL NOT IN OPERATOR
SQL BETWEEN OPERATOR
SQL NOT IN OPERATOR

 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

SELECT item, amount


FROM Orders
WHERE amount BETWEEN 300 AND 500;

 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

SELECT item, amount


FROM Orders
WHERE amount NOT 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

 The BETWEEN operator also works with text data.

-- 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.

-- get the records of those teams


-- who registered between given dates

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

 In SQL, the condition is used to select rows if the specified field


IS NULL

is NULL. It has the following syntax:


SELECT column1, column2, ...
FROM table WHERE column_name IS NULL;

column1, column2, ... are the table columns

table is the table name from where we select the data

column_name is the name of the column you want to check for NULL
IS NULL SYNTAX
-- select rows with NULL email values

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:

SELECT column1, column2, ...


FROM table WHERE column_name IS NOT NULL;

 column1, column2, ... are the table columns

 table is the table name from where we select the data

 column_name is the name of the column you want to check for NOT NULL
IS NOT NULL

-- select rows where email is not NULL


SELECT *
FROM Employee WHERE email IS NOT NULL;
 The above SQL query retrieves all the rows from
the Employee table where the value of the email column is NOT

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

 The syntax of the SQL MAX() function is:

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

SELECT MAX(age) AS max_age


FROM Customers;

 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,

--select the minimum value of first_name from Customers

SELECT MIN(first_name) AS min_first_name


FROM Customers;

 The SQL command selects the minimum value of first_name based on


the dictionary order.
MAX() AND MIN() WITH STRINGS
MAX() AND MIN() IN NESTED SELECT

 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;

 COUNT() is the function to count the number of rows in a table


 table is the name of the table
COUNT() SYNTAX

 Example: SQL COUNT()


--returns the number of rows in the Customers 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.

--returns the count of non-null values in the age column


SELECT COUNT(age)
FROM Customers;

 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.

-- count of customers who live in the UK

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,

-- count the unique countries in Customers table

SELECT COUNT(DISTINCT country)


FROM Customers;
 The SQL command returns the count of unique countries.
COUNT() WITH DISTINCT
SQL SUM() AND AVG()
IN SQL, THE SUM() AND AVG() FUNCTIONS ARE USED TO CALCULATE TOTAL AND AVERAGE VALUES IN
NUMERIC COLUMNS.
SQL SUM() FUNCTION

 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 the sum of amount from Orders table

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

SELECT SUM(amount) AS total_of_cus4


FROM Orders
WHERE customer_id = 4;

 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;

 AVG is the function that returns the aggregate of numeric values


 column_name is the column to which we apply the AVG function
 table is the name of the table to fetch the data from
SQL AVG() FUNCTION

-- get average age of customers


SELECT AVG(age) AS average_age
FROM Customers;

 The SQL command returns the average age of all customers.


SQL AVG() FUNCTION
EXAMPLE 2: SQL AVG() FUNCTION

--selects the average amount spent by each customer from the Orders table

SELECT customer_id, AVG(amount) AS average_spends


FROM Orders
GROUP BY customer_id;

 The SQL command returns the average spending of each customer.


EXAMPLE 2: SQL AVG() FUNCTION
EXAMPLE 2: SQL AVG() FUNCTION

You might also like