Lesson08-SQL Functions, Subqueries, and Joins
Lesson08-SQL Functions, Subqueries, and Joins
In this lesson, we shall learn how to use SQL built in functions, Sub queries, and Joins to answer more
complex questions(queries) as opposed to the simple queries described in the previous lesson.
Strings functions
String functions operate on string data types
Numeric functions
Numeric functions operate on numeric data types. The function include mathematical functions like
eg. round, log, sin, acos, cos, tan, abs etc
Example 8.2
SELECT ROUND(list_price,0) FROM products;
SELECT ROUND(list_price, -2) FROM products;
SELECT ABS(list_price) FROM products;
Date functions
Date functions operate on date data types .
Example 8.3
SELECT order_id,order_date FROM orders WHERE
YEAR(order_date)=‘2016’;
SELECT order_id,order_date FROM orders WHERE MONTH(order_date)=‘2’;
SELECT order_id,order_date FROM orders WHERE DAY(order_date)=‘18’;
SELECT order_id,order_date FROM orders WHERE WEEK(order_date)=‘48’;
1
SELECT order_id,order_date FROM orders WHERE
DAYOFWEEK(order_date)=‘monday’;
SELECT order_id,order_date FROM orders WHERE MONTHNAME(order_date)=
'march';
SELECT order_id,order_date FROM orders
WHERE DATEDIFF(required_date,order_date)<'1';
Aggregate functions
Aggregate functions operate on all of the data types and produce summarized result sets
Example 8.4
SELECT product_id ,COUNT(item_id) FROM order_items GROUP BY
product_id;
SELECT product_id ,SUM(list_price) FROM order_items GROUP BY
product_id;
SELECT AVG(list_price) FROM order_items;
SELECT model_year ,MAX(list_price), MIN(list_price FROM products
GROUP BY model_year;
Subqueries
A subquery is a SQL query nested inside a larger query.
A SELECT clause
A FROM clause
A WHERE clause
The subquery can be nested inside a SELECT, INSERT, UPDATE, or DELETE statement or inside
another subquery.
A subquery is usually added within the WHERE Clause of another SQL SELECT statement.
You can use the comparison operators, such as >, <, or =. The comparison operator can also be a
multiple-row operator, such as IN, ANY, or ALL.
A subquery is also called an inner query or inner select, while the statement containing a subquery is
also called an outer query or outer select.
The inner query executes first before its parent query so that the results of an inner query can be passed
to the outer query.
You can use a subquery in a SELECT, INSERT, DELETE, or UPDATE statement to perform the
following tasks:
2
Check whether the query selects any rows.
The above query list the IDs and names of lecturers who have been assigned courses.
Example 8.6
SELECT lect_id, lect_name FROM lecturers WHERE lect_id NOT IN
(SELECT lect_id FROM courses_offered WHERE lect_id IS NOT NULL);
3
The above query list the IDs and names of lecturers who have not been assigned any courses. (Recall
why omitting the last part (IS NOT NULL) in the subquery will render some invalid result.)
Joins
𝐀∩𝐁
The earlier example of listing of lecturers who have been assigned courses can be achieved using an
inner join as follows;
Example 8.7
SELECT L.lect_id, L.lect_name FROM lecturers L
INNER JOIN courses_offered C ON L.lect_id= C.lect_id;
Left Join
This join returns all rows from the left table, even if there are no matches in the right table.
𝐀 ∪ (𝐀 ∩ 𝐁) = 𝐀
Example 8.8
SELECT L.lect_id, L.lect_name,C.course_code FROM lecturers L
LEFT JOIN courses_offered C ON L.lect_id= C.lect_id;
Right Join
This join returns all rows from the right table, even if there are no matches in the left table.
𝐁 ∪ (𝐀 ∩ 𝐁) = 𝐁
4
Example 8.9
SELECT L.lect_id, L.lect_name,C.course_code FROM lecturers L
RIGHT JOIN courses_offered C ON L.lect_id= C.lect_id;
Full Join
This join returns rows when there is a match in one of the tables.
𝐀∪𝐁
Example 8.10
SELECT L.lect_id, L.lect_name FROM lecturers L
FULL JOIN courses_offered C ON L.lect_id= C.lect_id;
A FULL JOIN is not supported by MySQL, but is supported by other major SQL DBMS vendors like
Oracle and MSSQL.
The results of a FULL JOIN can be achieved in MySQL, by performing a UNION operation on LEFT
JOIN and RIGHT JOIN
Example 8.11
SELECT L.lect_id, L.lect_name,C.course_code FROM lecturers L
LEFT JOIN courses_offered C ON L.lect_id= C.lect_id
UNION
SELECT L.lect_id, L.lect_name,C.course_code FROM lecturers L
RIGHT JOIN courses_offered C ON L.lect_id= C.lect_id;
Self Join
This join returns used to join a table to itself as if the table were two tables, temporarily renaming at
least one table in the SQL statement. 𝑨 ∩ 𝑨′
This join is commonly used to find entities sharing similar attributes in a relation. For Example;
Example 8.12
SELECT L1.lect_id, L1.lect_name FROM lecturers L1
INNER JOIN lecturers L2 ON L1.lect_name= L2.lect_name
5
WHERE L1.lect_id<> L2.lect_id;
Cartesian Join
This join returns the Cartesian product of the sets of records from the two or more joined tables.
AxB
Example 8.13
SELECT L.lect_id, L.lect_name FROM lecturers L ,courses_offered C
WHERE L.lect_id= C.lect_id;
This JOIN is computationally more inefficient than the Inner, Left, and Right joins. Without the
WHERE clause, this join returns a multiplication of rows of the first and the second table; resulting to
irrelevant information -literally!
(i) List the Names of patrons who have never been issued a book.
(ii) List author, and title of books that have never been issued
(iii) List name, and email of patrons who were issued a book on ‘2021-11-16’
(iv) Update the status all books that have never been borrowed to ‘Archived’
(v) List name, and email of patrons who have ever been issued with a book authored by ‘Anthony
irungu’
6
Using the ERD, write SQL queries to ;
3. Recall our earlier case study on the “bikeshop” database and attempt to answer the following
questions using SQL queries.
i. How many orders are in the "orders" table?
ii. How many customers whose names begin with "Ja"?
iii. What is the average list price of the products?
iv. List all orders sorted by order date in descending order.
v. How many products of list price between 900 and 2000 and have orders on ‘2016-
02-14’?
vi. How many times has a product from the Ritchey brand been ordered by a customer
from New York (NY) state?
vii. List all products ordered by customers from the same city as the store where the
product is stocked?
viii. List the customer names and the number of orders made by each customer; but do
not show the cases where only one order was made.