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

Lesson 6 - SQL SELECT II

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

Lesson 6 - SQL SELECT II

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

SQL ORDER BY Clause

■ The ORDER BY clause in SQL is used to sort the result set in ascending
or descending order.
■ Example
-- orders all rows from Customers in ascending order by country
SELECT *
FROM Customers
ORDER BY country;

■ The SQL command selects all rows from the Customers table and then
sorts them in ascending order by country.
SQL ORDER BY Syntax

SELECT column1, column2, ...


FROM table
ORDER BY columnA, columnB, ...;

■ column1, column2, ... are the columns to be included in the result set
■ table is the name of the table from where the rows are selected
■ columnA, columnB, ... are the column(s) based on which the rows will
be ordered
SQL ORDER BY Syntax
ORDER BY ASC (Ascending
Order)
■ We can use the ASC keyword to explicitly sort selected records
in ascending order. For example,

-- orders all rows from Customers in ascending order by age


SELECT *
FROM Customers
ORDER BY age ASC;

■ The SQL command selects all the rows from Customers table and then
sorts them in ascending order by age.
ORDER BY ASC (Ascending
Order)
ORDER BY DESC (Descending
Order)

■ We use the DESC keyword to sort the selected records in descending


order. For example,
-- order all rows from Customers in descending order by age
SELECT *
FROM Customers
ORDER BY age DESC;
■ The SQL command selects all the customers and then sorts them in
descending order by age.
ORDER BY DESC (Descending
Order)
ORDER BY With Multiple Columns

■ We can also use ORDER BY with multiple columns. For example,

-- sort all rows from Customers, first by first_name and then by age
SELECT *
FROM Customers
ORDER BY first_name, age;

■ The SQL command selects all the records and then sorts them by
first_name. If the first_name repeats more than once, it sorts those
records by age.
ORDER BY With Multiple Columns
ORDER BY With WHERE

 We can also use ORDER BY with the SELECT WHERE clause. For example,
-- select last_name and age of customers who don't live in the UK
-- and sort them by last_name in descending order

SELECT last_name, age


FROM Customers
WHERE NOT country = 'UK’
ORDER BY last_name DESC;
 The SQL command first selects the last_name and age fields from the
Customers table if their country is not UK.
 Then, the selected records are sorted in descending order by their last_name.
ORDER BY With WHERE
SQL GROUP BY

■ In SQL, we use the GROUP BY clause to group rows based on the value
of columns.
■ Example
-- count the number of orders of each item
SELECT COUNT(order_id), item
FROM Orders
GROUP BY item;
SQL GROUP BY Syntax

■ In SQL, we use the GROUP BY clause to group rows based on the value of
columns.
■ Example
SELECT column1, column2, ...
FROM table GROUP BY columnA, columnB, ...;

■ column1, column2 ... are the columns of the table


■ table is the name of the table
■ columnA, columnB ... are the column(s) based on which the rows will be
grouped
Example: GROUP BY Number of
Customers in Each Country
-- count the number of customers in each country
SELECT country, COUNT(*) AS number
FROM Customers
GROUP BY country;

■ The SQL command groups the rows by the country column and counts
the number of each country (because of the COUNT() function).
Example: GROUP BY Number of
Customers in Each Country
Example: GROUP BY Amount
Spent By Each Customer
-- calculate the total amount spent by each customer
SELECT customer_id, SUM(amount) AS total
FROM Orders
GROUP BY customer_id;

■ The SQL command sums the amount after grouping rows by


customer_id.
Example: GROUP BY Amount
Spent By Each Customer
SQL GROUP BY Clause With JOIN

 We can also use the GROUP BY clause with the JOIN clause. For
example,
-- join the Customers and Orders tables
-- select customer_id and first_name from Customers table
-- also select the count of order ids from Orders table
-- group the result by customer_id

SELECT Customers.customer_id, Customers.first_name, Count(Orders.order_id) AS order_count


FROM Customers
LEFT JOIN Orders
ON Customers.customer_id = Orders.customer_id
GROUP BY Customers.customer_id;

 The SQL command joins the Customers and Orders tables and groups
the result set by customer_id (a customer).
SQL LIKE and NOT LIKE
Operators
 We use the SQL LIKE operator with the WHERE clause to get a result
set that matches the given string pattern.
 Example

-- select customers who live in the UK


SELECT first_name
FROM Customers
WHERE country LIKE 'UK';
 The SQL command selects the first name of customers whose country
is UK.
SQL LIKE Syntax

SELECT column1, column2, ...


FROM table
WHERE column LIKE value;

■ column1,column2, ... are the columns to select the data from


■ table is the name of the table
■ column is the column we want to apply the filter to
■ LIKE matches the column with value
■ value is the pattern you want to match in the specified column
Example: SQL LIKE

-- select customers who live in the UK


SELECT *
FROM Customers
WHERE country LIKE 'UK';

■ The SQL command selects customers whose country is UK.


Example: SQL LIKE
SQL NOT LIKE Operator

 We can also invert the working of the LIKE operator by using the NOT operator
with it. This returns a result set that doesn't match the given string pattern.

SELECT column1, column2, ...


FROM table_name
WHERE column NOT LIKE value;

■ column1,column2, ... are the columns to select the data from


■ table_name is the name of the table
■ column is the column we want to apply the filter to
■ NOT LIKE ignores the match of the column with the value
■ value is the pattern you don't want to match in the specified column
SQL NOT LIKE Operator

 For example,
-- select customers who don't live in the USA
SELECT *
FROM Customers
WHERE country NOT LIKE 'USA';
■ The SQL command selects all customers except those whose country
is USA.
SQL LIKE With Multiple Values

 For example,
■ We can use the LIKE operator with multiple string patterns using the OR
operator. For example,
-- select customers whose last_name starts with R and ends with t
-- or customers whose last_name ends with e
SELECT *
FROM Customers
WHERE last_name LIKE 'R%t' OR last_name LIKE '%e';
■ The SQL command selects customers whose last_name starts with R
and ends with t or customers whose last_name ends with e.
SQL Wildcards

 A wildcard character in SQL is used with the LIKE clause to replace a single
character or a set of characters in a string.% and _ are two commonly used
wildcard characters in SQL.
 Example
-- select customers who live in countries -- that start with 'US' followed by a single
character
SELECT *
FROM Customers
WHERE country LIKE 'US_'
 _ is a wildcard character that represents exactly one character after a string.
 So, the SQL query selects customers whose country starts with US and ends
with a single character after it.
SQL Wildcard Syntax

SELECT column1, column 2, ...


FROM table
WHERE column LIKE 'Wildcard String';

 column1, column2, ... are the columns to select the data from
 table is the name of the table
 column is the column we want to apply the filter to
 LIKE matches the column with Wildcard String
 Wildcard String is a combination of strings and wildcard characters
SQL Wildcard Syntax

 For example,
-- select rows where the last name -- of customers start with R
SELECT *
FROM Customers
WHERE last_name LIKE 'R%';

 % (zero or more characters) is a wildcard. So, the SQL command


selects customers whose last_name starts with R followed by zero or
more characters after it.
SQL Wildcard Syntax
% Wildcard in SQL

 The % wildcard in SQL is used to represent zero or more characters.


For example,
-- select rows where the first names -- of customers start with J
SELECT *
FROM Customers
WHERE first_name LIKE 'J%';

 The SQL command selects customers whose last name starts


with J followed by zero or more characters
_ Wildcard in SQL

 The _ wildcard in SQL is used to represent exactly one character in a


string.
-- select
For example,
customers whose countries start with U -- followed by a single character
SELECT *
FROM Customers
WHERE country LIKE 'U_';

 The SQL command selects customers


Expressionwhose country
String name starts
Matched?
with U and is followed by only one character.
U no match

U_ UK match

USA no match
[] Wildcard in SQL
 The [] wildcard in SQL is used to represent any one character inside
brackets.
 select
-- For example,
customers with country that starts with UK or UA -- and is followed by any
number of characters
SELECT *
FROM Customers
WHERE country LIKE 'U[KA]%';

 The SQL command selects customers whose country name starts with
U and isExpression
followed by either String
K or A and any number of characters
Matched?
afterward.
U no match

UK match
U[KA]%
UAE match

USA no match
! Wildcard in SQL
 The ! wildcard in SQL is used to exclude characters from a string. For
example,
--select rows where customer's last names don't start with D or R
SELECT *
FROM Customers
WHERE last_name LIKE '[!DR]%';

 The SQL command selects customers whose last_name does not start
Expression String Matched?
with D or R.
Doe no match

Reinhardt no match

Luna match
[!DR]%
D no match

O match

R no match
SQL UNION
 The UNION operator selects fields from two or more tables.
-- select the union of name columns from two tables Teachers and Students
SELECT name
FROM Teachers
UNION
SELECT name
FROM Students;
 The SQL command selects the union of the name columns from two
different tables: Teachers and Students.
SQL Union Syntax
SELECT column1, column2, ...
FROM table1
UNION
SELECT column1, column2, ...
FROM table2;

 column1,column2, ... are the column names required for the union
 table1 and table2 are the names of the tables to fetch the columns from
 UNION combines the columns in the tables
Example: SQL UNION
-- select the union of age columns from two tables Teachers and Students
SELECT age
FROM Teachers
UNION
SELECT age
FROM Students;

 The SQL command returns the age columns from the Teachers and
the Students tables, ignoring the duplicate fields.
Example: SQL UNION
Example: SQL UNION With WHERE Clause

-- select the union of age columns from both Teachers and Students tables where age >=
20
SELECT age, name FROM Teachers
WHERE age >= 20
UNION
SELECT age, name FROM Students
WHERE age >= 20;
 The SQL command selects the age column (only the unique values)
from both tables where the age is greater than or equal to 20.
SQL UNION ALL Operator
 The UNION ALL operator selects fields from two or more tables similar
to UNION. However, unlike UNION, UNION ALL doesn't ignore duplicate fields.
 Let's try the previous SQL command again using UNION ALL instead of UNION.

-- select the union of age from Teachers and Students tables


SELECT age FROM Teachers
UNION ALL
SELECT age
FROM Students;
 The SQL command selects fields from both tables, including the duplicate
fields.
SQL UNION ALL Operator
Example: SQL UNION ALL With WHERE
Clause
-- select the union of age columns from both Teachers and Students tables where age
>= 20
SELECT age, name FROM Teachers
WHERE age >= 20
UNION ALL
SELECT age, name FROM Students
WHERE age >= 20;

 The SQL command selects the age column from both tables (including
duplicate values) where the age is greater than or equal to 20.
SQL Subquery
 In SQL, a SELECT statement may contain another SQL statement, known
as a subquery or nested query. Example,
-- use a subquery to select the first name of customer -- with the highest age
SELECT first_name
FROM Customers
WHERE age= (
-- subquery
SELECT MAX(age)
FROM CUSTOMERS );

 The query is divided into two parts:


 the subquery selects the maximum age from the Customers table
 the outer query selects the first_name of the customer with the
maximum id (returned by the subquery)
SQL Subquery Syntax

SELECT column FROM table


WHERE column OPERATOR (
SELECT column FROM table
);

 column is the name of the column(s) to filter


 OPERATOR is any SQL operator to connect the two queries
 table is the name of the table to fetch the column from
SQL Subquery Syntax
-- select all the rows from the Customers table -- with the minimum age
SELECT *
FROM Customers
WHERE age = (
SELECT MIN(age)
FROM Customers
);
SQL Subquery Syntax
SQL Subquery Syntax
 In a subquery, the outer query's result depends on the result set of the
inner subquery. That's why subqueries are also called nested queries.
 Here is how this code works:
 executes the subquery first (inner query), and returns the minimum
age 22
 executes the outer query, and selects customers with age 22
SQL Views
■ In SQL, views contain rows and columns similar to a table, however, views
don't hold the actual data.
■ You can think of a view as a virtual table environment that's created from
one or more tables so that it's easier to work with data.
Creating a View in SQL
■ We can create views in SQL by using the CREATE VIEW command. For
example,
CREATE VIEW us_customers AS
SELECT customer_id, first_name
FROM Customers
WHERE Country = 'USA';

■ Here, a view named us_customers is created from the customers table.

■ Now to select the customers who lives in USA, we can simply run,
SELECT * FROM us_customers;
Updating a View
■ It's possible to change or update an existing view using the CREATE OR
REPLACE VIEW command.
■ For example,
CREATE OR REPLACE VIEW us_customers AS
SELECT *
FROM Customers
WHERE Country = 'USA';

■ The us_customers view is updated to show all the fields.


Deleting a View

 We can delete views using the DROP VIEW command. For example,

DROP VIEW us_customers;


Views for Complex Queries
 Suppose A and B are two tables and we wan't to select data from both of
the tables. For that, we have to use SQL JOINS.
 However using the JOIN each time could be a tedious task. For that, we
can create a view to fetch records easily.
 Let's create a view,
CREATE VIEW order_details AS
SELECT Customers.customer_id, Customers.first_name, Orders.amount
FROM Customers JOIN Orders
ON Customers.customer_id = Orders.customer_id;
Views for Complex Queries
 Now, to select the data, we can run

SELECT *
FROM order_details;

 The SQL command selects data from the view order_details.


SQL CTE (Common Table Expressions)
 Common Table Expressions (CTE) is a temporary result set in SQL that we
can reference within a SELECT, INSERT, UPDATE, or DELETE statement.
 CTEs make complex queries more readable and maintainable.
 Example
WITH RecentCustomers AS (
SELECT * FROM Customers WHERE age < 30
)
SELECT * FROM RecentCustomers;

 The CTE RecentCustomers selects customers younger than 30 years old


from the Customers table.
CTE With WHERE Clause
 CTEs can be used in conjunction with a WHERE clause to filter results in a
more structured way.
 For example,
WITH HighValueOrders AS (
SELECT * FROM Orders
WHERE amount > 1000
)
SELECT * FROM HighValueOrders
WHERE customer_id = 3;
 The CTE HighValueOrders selects orders with an amount greater
than 1000.
 Then, the query further filters these to orders made by the customer with
ID 3.
Using CTEs With JOIN
 CTEs can be effectively used with JOIN to simplify complex joins across
multiple tables.
WITH CustomerOrders AS (
SELECT C.customer_id, O.item
FROM Customers C
JOIN Orders O ON C.customer_id = O.customer_id )
SELECT * FROM CustomerOrders;

 In this example, the


CTE CustomerOrders joins Customers and Orders tables and displays
the customer_id column from the Customers table and the item column
from the Orders table.
 The query then selects all the columns from the CustomerOrders CTE.
Using CTEs With UPDATE Statement
 TEs can also be used within UPDATE statements for updating data based
on complex criteria.
 For example,
WITH PendingShippings AS (
SELECT * FROM Shippings
WHERE status = 'Pending’
)
UPDATE Shippings
SET status = 'In Transit’
WHERE shipping_id IN (SELECT shipping_id FROM PendingShippings);

SELECT * FROM Shippings;

 The CTE PendingShippings selects all shippings whose status value is Pending.
 The UPDATE statement then updates these shippings to In Transit.
SQL ANY AND ALL
SQL ANY Operator
 SQL ANY compares a value of the first table with all values of the second table and
returns the row if there is a match with any value.
 It has the following syntax:
SELECT column
FROM table1
WHERE column OPERATOR ANY (
SELECT column
FROM table2 );

 column is the name of the column(s) to filter


 table1 and table2 are the two tables to compare
 OPERATOR is any SQL operator to connect the two queries
 ANY compares table1 and table2 to see if there are any matches
Example 1: SQL ANY Operator
 Suppose we want to find teachers whose age is similar to any of the
student's age. Then, we can use the following query:
SELECT *
FROM Teachers
WHERE age = ANY (
SELECT age
FROM Students );

 Here, the subquery returns all the ages from the Students table.
SELECT age FROM Students
 And, the condition below compares the student ages (returned by
subquery) with the ages of the teachers.
WHERE age = ANY (...)

 If there is any match, the corresponding row of the Teachers table is


Example 1: SQL ANY Operator
Example 2: SQL ANY With the < Operator

 We can use any comparison operators like =, >, <, etc., with the ANY and
ALL keywords.
 Let's look at an example where we want teachers whose age is less than
any* student.
SELECT
FROM Teachers
WHERE age < ANY (
SELECT age
FROM Students );

 The SQL command selects rows if age in the outer query is less than any
age in a subquery.
Example 2: SQL ANY With the < Operator
SQL ALL Operator
 SQL ALL compares a value of the first table with all values of the second
table and returns the row if there is a match with all values.

SELECT column
FROM table1
WHERE column OPERATOR ALL (
SELECT column
FROM table2 );

 column is the name of the column(s) to filter


 table1 and table2 are the two tables to compare
 OPERATOR is any SQL operator to connect the two queries
 ALL compares table1 and table2 to see if all the values match
Example 3: SQL ALL Operator
 For example, if we want to find teachers whose age is greater than all students,
we can
SELECT * use
FROM Teachers
WHERE age > ALL (
SELECT age
FROM Students );

 Here,
SELECT agethe subquery
FROM Studentsbelow returns all the ages from the Students table.

 And, the condition below compares the student ages (returned by subquery)
with
WHERE the
age ages
> ALL (...)of the teachers.

 If the teacher's age is greater than all student's ages, the corresponding row of
the Teachers table is selected.
Example 3: SQL ALL Operator
SQL CASE
 The SQL CASE statement evaluates a list of conditions and adds a column
with values based on the condition. For example,

-- add a new column 'order_volume' in the Orders table


-- and flag any order greater than 10000 as 'Large Order'
-- and smaller than 10000 as 'Small Order'
SELECT *,
CASE
WHEN amount >= 10000 THEN 'Large Order’
WHEN amount < 10000 THEN 'Small Order’
END AS 'order_volume’
FROM Orders;

 The result set has a new column, order_volume which labels the rows with
amounts greater than or equal to 10000 as Large Order and smaller than
10000 as Small Order.
SQL CASE Syntax
SELECT column1, column2,... ,
CASE
WHEN condition THEN result END AS alias_name
FROM table;

 column1,column2, ... are the column names to be included in the result


set
 CASE checks the condition
 result is the result or value to be inserted to the new column if condition is
satisfied
 END ends the CASE statement
 AS specifies the name alias_name for the new column
 table is the name of the table.
Example: Voter Eligibility Using SQL CASE

-- add a new column 'can_vote' to Customers table


-- insert 'Allowed' into it if customer is older than 17
SELECT customer_id, first_name,
CASE
WHEN age >= 18 THEN 'Allowed' END AS can_vote
FROM Customers;

 The SQL command checks each row with the given case. The result set
contains:
 values from customer_id and first_name columns
 a new can_vote column with value Allowed if age is greater than 18,
otherwises empty
Example: Voter Eligibility Using SQL CASE
Example: SQL CASE to Calculate the
Discount Amount
 Let's take a look at another example where we want to provide a 10%
discount on each order for a Christmas sale if the amount is 400 or
more.
SELECT order_id, customer_id,
CASE
WHEN amount >= 400 THEN (amount - amount * 10/100)
END AS offer_price
FROM Orders;

 The CASE statement checks if the amount is greater than or equal to 400. If
this condition is satisfied, a new column offer_price will contain the values
equal to amount - amount * 10/100.
CASE With Multiple Conditions
 It is also possible to stack multiple conditions inside a single CASE clause.
SELECT column1, column2, ...
CASE
WHEN condition1 THEN result1
WHEN condition2 THEN result2
-- Add more WHEN conditions and results as needed
END AS alias_name
FROM table_name;
 We can add as many WHEN ... THEN conditions as required in
the CASE statement. For example,
-- multiple CASE conditions in SQL
SELECT customer_id, first_name,
CASE
WHEN country = 'USA' THEN 'United States of America’
WHEN country = 'UK' THEN 'United Kingdom’
END AS country_name
FROM Customers;
CASE With Multiple Conditions
-- multiple CASE conditions in SQL
SELECT customer_id, first_name,
CASE
WHEN country = 'USA' THEN 'United States of America’
WHEN country = 'UK' THEN 'United Kingdom’
END AS country_name
FROM Customers;

 The result set contains a new column, country_name along with


customer_id and first_name.
 The value of country_name becomes:
 United States of America if the country is equal to USA
 United Kingdom if the country is equal to UK
CASE With ELSE
 A CASE statement can have an optional ELSE clause. The ELSE clause is
executed if none of the conditions in the CASE statement is matched.
SELECT customer_id, first_name,
CASE
WHEN condition1 THEN result1
WHEN condition2 THEN result2
-- Add more WHEN conditions and results as needed
ELSE else_result
END AS alias_name
FROM table_name;

 The ELSE clause has no condition as it is executed if none of


the WHEN conditions are matched. For example,
CASE With ELSE
-- CASE condition with ELSE clause in SQL
SELECT customer_id, first_name,
CASE
WHEN country = 'USA' THEN 'United States of America’
WHEN country = 'UK' THEN 'United Kingdom’
ELSE 'Unknown Country’
END AS country_name
FROM Customers;

 The result set contains a new column, country_name along


with customer_id and first_name.
 The value of country_name becomes:
 United States of America if country is USA
 United Kingdom if country is UK
 Unknown Country if country is neither USA nor UK (because of
the ELSE clause).
CASE With ELSE
SQL HAVING Clause
 The SQL HAVING clause is used if we need to filter the result set based on
aggregate functions such as MIN() and MAX(), SUM() and AVG(),
and COUNT().
--select
Example
customers with the same first name based on their age count
SELECT COUNT(age) AS Count, first_name
FROM Customers
GROUP BY first_name
HAVING COUNT(age) > 1;

 The SQL command


 counts the age of each row and groups them by first_name
 returns the result set if the count of age is greater than 1 (thus filtering
out customers with the same first_name)
SQL HAVING Syntax

SELECT AggFunc(column), extra_columns


FROM table
GROUP BY target_column
HAVING condition

 AggFunc(column) refers to any aggregate function applied to a column


 extra_columns are other extra columns to filter
 GROUP BY groups the data by target_column
 HAVING condition compares the column to certain conditions that require
filtering
Example: SQL HAVING
-- select the count of customer ids greater than one and their corresponding country
SELECT COUNT(customer_id), country
FROM Customers
GROUP BY country
HAVING COUNT(customer_id) > 1;

 The SQL command:


 counts the number of rows by grouping them by country
 returns the result set if their count is greater than 1.
Example: SQL HAVING
SQL HAVING vs. WHERE

HAVING Clause WHERE Clause

The HAVING clause checks The WHERE clause checks


the condition on a group the condition on each
of rows. individual row.

The WHERE clause cannot


HAVING is used with
be used with aggregate
aggregate functions.
functions.

The HAVING clause is The WHERE clause is


executed after the GROUP executed before the
BY clause. GROUP BY clause.
SQL HAVING vs. WHERE
 We can write a WHERE clause to filter out rows where the value of amount in
the Orders table is less than 500:

SELECT customer_id, amount


FROM Orders
WHERE amount < 500;

 But with the HAVING clause, we can use an aggregate function


like SUM to calculate the sum of amounts in the order table and get the
total order value of less than 500 for each customer:
SELECT customer_id, SUM(amount) AS total
FROM Orders
GROUP BY customer_id
HAVING SUM(amount) < 500;
SQL EXISTS Operator
 The SQL EXISTS operator tests the existence of any value in a subquery i.e. it
executes the outer SQL query only if the subquery is not NULL (empty result-set).
 Example

-- select customer id and first name of customers


-- whose order amount is less than 12000
SELECT customer_id, first_name
FROM Customers WHERE EXISTS (
SELECT order_id
FROM Orders
WHERE Orders.customer_id = Customers.customer_id AND amount < 12000 );

 checks for the order_id of customers in the Orders table where amount is less
than 12000
 returns the customer_id and first_name of customers from the Customers table
who have made a purchase of less than 12000
SQL Exists Syntax

SELECT column1, column2, ...


FROM table
WHERE EXISTS(SUBQUERY);

 column1, column2, ... are the column names to filter


 table refers to the name of the table
 EXISTS tests the result of the subquery
 SUBQUERY can be any SQL query
SQL Exists SyntaxExample 1: SQL Exists

-- select customer id and first name of customers


from Customers table
-- if the customer id exists in the Orders table
SELECT customer_id, first_name
FROM Customers
WHERE EXISTS (
SELECT order_id
FROM Orders
WHERE Orders.customer_id =
Customers.customer_id );
SQL Exists SyntaxExample 1: SQL Exists
SQL NOT EXISTS
 We can also use the NOT operator to inverse the working of
the EXISTS clause. The SQL command executes if the subquery returns an
empty result-set.
-- select customer id and first name from Customers table
-- if the
Forcustomer
example,id doesn't exist in the Orders table
SELECT customer_id, first_name
FROM Customers
WHERE NOT EXISTS (
SELECT order_id
FROM Orders
WHERE Orders.customer_id = Customers.customer_id );

 The SQL command returns a row from the Customers table if the related
row is not in the Orders table.

You might also like