0% found this document useful (0 votes)
12 views6 pages

BE23.0 SQL Day 3 Pre-Work

This document covers SQL concepts related to MySQL, focusing on TIME functions and subqueries. It explains how to use various subquery types within SQL statements, including those with comparison operators, IN/NOT IN, and EXISTS/NOT EXISTS. Additionally, it provides examples demonstrating how to implement these concepts in practical SQL queries.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views6 pages

BE23.0 SQL Day 3 Pre-Work

This document covers SQL concepts related to MySQL, focusing on TIME functions and subqueries. It explains how to use various subquery types within SQL statements, including those with comparison operators, IN/NOT IN, and EXISTS/NOT EXISTS. Additionally, it provides examples demonstrating how to implement these concepts in practical SQL queries.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Back-End v23.

0
Dashboard / My courses / BE23.0 / MySQL v23.0 / Day 3 | Pre-work / SQL | Day 3 | Pre-Work

 Back to 'Day 3 | Pre-work '


SQL | Day 3 | Pre-Work

MySQL
DAY 3
Table of contents:
TIME functions
CURRENT_DATE()
CURRENT_TIME()
CURRENT_TIMESTAMP()
SQL Subquery
SQL subquery within a WHERE clause
SQL subquery with comparison operators
SQL subquery with IN and NOT IN operators
SQL subquery with EXISTS and NOT EXISTS
SQL subquery in FROM clause
Please note that many functions mentioned in this chapter are
synonyms of other functions. If you would like to read up on them in
more detail, the official MySQL documentation has all the
information you need.

TIME functions

CURRENT_DATE()

SELECT CURRENT_DATE();

The date is returned as "YYYY-MM-DD" (string)

CURRENT_TIME()
SELECT CURRENT_TIME();

The time is returned as "HH-MM-SS" (string)

CURRENT_TIMESTAMP()

SELECT CURRENT_TIMESTAMP();

The date and time is returned as "YYYY-MM-DD HH-MM-SS" (string).

SQL Subquery

Until now you used simple SQL CRUD queries, now we will put things on the next level.
A subquery is a SQL query that is nested inside another SQL query
such as SELECT, INSERT, UPDATE or DELETE. This nesting can go
many levels “deep”, so a SQL subquery can be nested within another
subquery.
Another name for the subquery is inner query, while the query that
contains another query is often called an outer query.

Let’s take a look at the following subquery that returns employees who are located in the offices in the USA.
● The subquery will give us all office codes where the country matches the USA.
● Following this, the outer query selects both the first and last name of people that are employees of those
previously returned offices.

SELECT last_name, first_name


FROM employees
WHERE office_code IN (SELECT office_code
FROM offices
WHERE country = 'USA');

You can use a subquery anywhere that you use an expression. In addition, you must enclose a subquery in
parentheses.

SQL subquery within a WHERE clause


We will use the payments table for the demonstration.

book_orders
* customer_id
* invoice_number
due_date
amount

SQL subquery with comparison operators


As we already know, using comparison operators e.g., =, >, <, etc. we can compare values returned by the subquery.
This is done using the operator in the WHERE clause.

Let's say we want to find out which customer has had the greatest amount to pay from our book orders:

SELECT customer_id,
invoice_number,
amount
FROM book_orders
WHERE amount = (
SELECT MAX(amount)
FROM book_orders
);

From this, we could get the following result:

customer_id invoice_number amount


> 15 AT1232021 874.84

As mentioned, we could use comparison operators to refine our search. Let's say that we want to find those
customers that have spent more than the average amount in our store to send them a gift certificate.
To achieve this, we first use a subquery to calculate the average payment using the AVG aggregate function. Then, in
the outer query, we select those payments that are greater than the average payment returned by the subquery.
SELECT customer_id,
invoice_number,
amount
FROM book_orders
WHERE amount > (
SELECT AVG(amount)
FROM book_orders
);

customer_id invoice_number amount


> 8 AT0152021 327,07
12 AT1052021 335,23
12 AT172021 660,12
21 AT1292021 722,98
21 AT342021 819,08
21 AT1382021 609,61
69 AT492021 42,77
79 AT692021 582,35

SQL subquery with IN and NOT IN operators


If a subquery returns more than one value, you can use other operators such as IN or NOT IN operator in the WHERE
clause.
See the following customers and orders tables.

customers book_orders

• customer_id order_id
customer_full_name order_date
customer_first_name shipment_date
customer_last_name status
address notes
city customer_id
zip_code
country
customer_care_rep_id

If we for example want to find a list of all customers, who’s have not yet ordered anything we can use a subquery with
NOT IN operator like so:

SELECT customer_full_name
FROM customers
WHERE customer_id NOT IN(
SELECT DISTINCT customer_id
FROM orders
);

customer_full_name
> Alex Hunter
Arno Dorian
Demitri Maximoff
Jin Kazama
Kyle Katarn
Garrus Vakarian
Kung Lao
Damon Baird
SQL subquery with EXISTS and NOT EXISTS
If we want to find out if entries do or do not exist in our databases, we use the EXISTS or NOT EXISTS operator
which then will return a Boolean value of either TRUE or FALSE.

Let's say we want a list of customers who have one or more orders for more than 10.000. As a first step, we build a
query that checks if there is at least one order with total sales greater than 10,000:

SELECT price_each /item_price * quantity_ordered /amount_ordered


FROM order_details
WHERE
price_each /item_price * quantity_ordered /amount_ordered> 10000
GROUP BY
order_id;

We use the GROUP BY statement together with aggregate functions (COUNT, MAX, MIN, SUM, AVG) to group the
result-set by one or more columns.

price_each /item_price * quantity_ordered /amount_ordered


> 10286.40
11503.14
10460.16
11170.52
10723.60
10072.00

Since we now see that such orders exist, when we use this as a subquery the outer query will return all customers:

SELECT
customer_name
FROM
customers
WHERE
EXISTS (
SELECT
item_price * amount_ordered
FROM
orderdetails
WHERE
itemPrice * amount_ordered > 10000
GROUP BY
order_id
);

customer_name
> Sly Cooper
> Corvo Attano
> Damon Baird
> Lee Chaolan
> Glass Joe
> Kung Lao

Of course, if you replace the EXISTS with NOT EXIST, the query will not return any records at all.

SQL subquery in FROM clause


This subquery will return results in the form of a new table, called either a derived table or materialized subquery.
Let's try to find both the minimum, maximum as well as average number of ordered items:
SELECT
MAX(items),
MIN(items),
FLOOR(AVG(items))
FROM
(
SELECT
order_id,
COUNT(order_id) AS items
FROM
order_details
GROUP BY
orderUD
) AS line_items;

*FLOOR will round down the result, meaning it will return the largest integer value that is smaller than or equal to
what it is provided.

MAX(items) MIN(items) FLOOR(AVG(items))


> 26 1 13

As you can see, the subquery returns the result set as a derived table for the outer query.

◀︎MySQL | Day 2 | CLASSWORK


Jump to...
MySQL Day 3 | Quiz ▶︎

You are logged in as Rafael Braga-Kribitz (Log out)


BE23.0
Data retention summary

You might also like