SQL For Beginners LearnProgrammingAcademy
SQL For Beginners LearnProgrammingAcademy
SHOW DATABASES;
USE coffee_store;
SHOW TABLES;
1
-- 3.5 Modifying Tables: Adding and Removing Columns
USE coffee_store;
DESCRIBE products;
USE example;
DESCRIBE test;
SHOW TABLES;
2
-- 3.7 Truncating Tables
USE example;
DESCRIBE test;
SHOW TABLES;
INSERT INTO test (name, age) VALUES('Ben', 19), ('Simon', 28), ('Claire', 23);
SELECT * FROM test;
3
-- Section 4: More on Alter Table
-- 4-2 Creating our Test Database
USE test;
SHOW TABLES;
4
-- 4-3 Add and Remove Primary Key
/*
*/
USE test;
DESCRIBE addresses;
DESCRIBE people;
5
-- 4-4 Add and Remove Foreign Key
/*
*/
USE test;
DESCRIBE addresses;
DESCRIBE people;
6
-- 4-5 Add Unique Constraint
/*
*/
USE test;
DESCRIBE pets;
7
-- 4-6 Change Column Name
/*
*/
USE test;
DESCRIBE pets;
8
-- 4-7 Change Column Data Type
/*
*/
USE test;
DESCRIBE addresses;
9
-- Section 5: Data Manipulation Language
-- 5-2 Inserting Data into Tables
/*
*/
USE coffee_store;
SHOW tables;
10
-- 5-3 Updating Data in Tables
/*
*/
USE coffee_store;
UPDATE products
SET coffee_origin = 'Sri Lanka'
WHERE id = 7;
UPDATE products
SET price = 3.25, coffee_origin = 'Ethiopia'
WHERE name = 'Americano';
UPDATE products
SET coffee_origin = 'Colombia'
WHERE coffee_origin = 'Brazil';
11
-- 5-4 Deleting Data from Tables
/*
*/
USE example;
DESCRIBE people;
# noinspection SqlWithoutWhere
DELETE FROM people;
12
-- 5-4 Completing the Coffee Store Database
USE coffee_store;
13
(1, 11, '2022-04-15 [Link]'), (4, 12, '2022-04-16 [Link]'), (6, 18, '2022-05-02 [Link]'),
(3, 22, '2022-05-13 [Link]'), (3, 14, '2022-05-19 [Link]'), (6, 2, '2022-05-19 [Link]'),
(3, 12, '2022-05-30 [Link]'), (6, 15, '2022-06-04 [Link]'), (7, 23, '2022-06-10 [Link]'),
(4, 18, '2022-06-11 [Link]'), (5, 10, '2022-06-14 [Link]'), (4, 24, '2022-06-14 [Link]'),
(2, 4, '2022-06-14 [Link]'), (3, 18, '2022-06-15 [Link]'), (1, 11, '2022-06-15 [Link]'),
(4, 4, '2022-06-16 [Link]'), (6, 20, '2022-06-19 [Link]'), (7, 3, '2022-06-20 [Link]'),
(5, 19, '2022-06-21 [Link]'), (1, 24, '2022-06-24 [Link]'), (5, 13, '2022-06-24 [Link]'),
(5, 3, '2022-06-27 [Link]'), (2, 8, '2022-06-30 [Link]'), (1, 12, '2022-07-01 [Link]'),
(5, 4, '2022-07-03 [Link]'), (7, 18, '2022-07-05 [Link]'), (1, 18, '2022-07-08 [Link]'),
(4, 14, '2022-07-13 [Link]'), (4, 1, '2022-07-18 [Link]'), (7, 4, '2022-07-18 [Link]'),
(7, 5, '2022-07-21 [Link]'), (7, 18, '2022-07-24 [Link]'), (5, 23, '2022-07-25 [Link]'),
(1, 3, '2022-07-26 [Link]'), (1, 24, '2022-07-28 [Link]'), (7, 20, '2022-08-07 [Link]'),
(2, 14, '2022-08-08 [Link]'), (7, 24, '2022-08-12 [Link]'), (3, 10, '2022-08-13 [Link]'),
(6, 8, '2022-08-20 [Link]'), (7, 10, '2022-08-21 [Link]'), (1, 5, '2022-08-24 [Link]'),
(6, 14, '2022-08-25 [Link]'), (2, 20, '2022-08-29 [Link]'), (2, 12, '2022-08-31 [Link]'),
(1, 1, '2022-09-03 [Link]'), (6, 13, '2022-09-05 [Link]'), (6, 14, '2022-09-06 [Link]'),
(1, 1, '2022-09-08 [Link]'), (7, 13, '2022-09-12 [Link]'), (1, 1, '2022-09-14 [Link]'),
(3, 16, '2022-09-15 [Link]'), (6, 4, '2022-09-15 [Link]'), (2, 4, '2022-09-17 [Link]'),
(2, 7, '2022-09-23 [Link]'), (7, 8, '2022-09-25 [Link]'), (2, 21, '2022-09-26 [Link]'),
(6, 15, '2022-09-28 [Link]'), (7, 12, '2022-09-29 [Link]'), (5, 14, '2022-09-29 [Link]'),
(7, 23, '2022-10-01 [Link]'), (1, 5, '2022-10-01 [Link]'), (2, 3, '2022-10-10 [Link]'),
(2, 9, '2022-10-10 [Link]'), (7, 7, '2022-10-12 [Link]'), (6, 22, '2022-10-12 [Link]'),
(1, 7, '2022-10-13 [Link]'), (3, 12, '2022-10-14 [Link]'), (5, 17, '2022-10-15 [Link]'),
(5, 24, '2022-10-15 [Link]'), (2, 3, '2022-10-15 [Link]'), (7, 5, '2022-10-16 [Link]'),
(3, 5, '2022-10-18 [Link]'), (3, 15, '2022-10-24 [Link]'), (6, 18, '2022-10-29 [Link]'),
(7, 21, '2022-10-31 [Link]'), (3, 14, '2022-11-01 [Link]'), (4, 11, '2022-11-04 [Link]'),
(6, 14, '2022-11-04 [Link]'), (3, 23, '2022-11-04 [Link]'), (6, 20, '2022-11-12 [Link]'),
(5, 5, '2022-11-16 [Link]'), (2, 13, '2022-11-18 [Link]'), (2, 14, '2022-11-20 [Link]'),
(7, 24, '2022-11-25 [Link]'), (5, 7, '2022-11-29 [Link]'), (6, 7, '2022-12-03 [Link]'),
(3, 9, '2022-12-04 [Link]'), (2, 19, '2022-12-05 [Link]'), (6, 15, '2022-12-07 [Link]'),
(2, 16, '2022-12-13 [Link]'), (1, 22, '2022-12-14 [Link]'), (4, 24, '2022-12-16 [Link]'),
(2, 17, '2022-12-18 [Link]'), (1, 13, '2022-12-18 [Link]'), (1, 20, '2022-12-18 [Link]'),
(2, 16, '2022-12-20 [Link]'), (5, 7, '2022-12-31 [Link]'), (2, 14, '2022-12-31 [Link]'),
(5, 15, '2023-01-01 [Link]'), (3, 22, '2023-01-01 [Link]'), (6, 19, '2023-01-08 [Link]'),
(3, 22, '2023-01-09 [Link]'), (3, 10, '2023-01-09 [Link]'), (7, 16, '2023-01-10 [Link]'),
(4, 3, '2023-01-11 [Link]'), (2, 20, '2023-01-12 [Link]'), (5, 21, '2023-01-13 [Link]'),
(3, 24, '2023-01-13 [Link]'), (6, 6, '2023-01-14 [Link]'), (7, 24, '2023-01-15 [Link]'),
(4, 2, '2023-01-17 [Link]'), (2, 19, '2023-01-17 [Link]'), (4, 18, '2023-01-19 [Link]'),
(3, 20, '2023-01-25 [Link]'), (1, 24, '2023-01-31 [Link]'), (3, 6, '2023-01-31 [Link]'),
(7, 19, '2023-02-01 [Link]'), (2, 10, '2023-02-05 [Link]'), (3, 19, '2023-02-09 [Link]'),
(3, 16, '2023-02-12 [Link]'), (2, 21, '2023-02-12 [Link]'), (2, 24, '2023-02-14 [Link]'),
(3, 5, '2023-02-15 [Link]'), (3, 9, '2023-02-16 [Link]'), (2, 20, '2023-02-17 [Link]'),
(4, 2, '2023-02-20 [Link]'), (7, 3, '2023-02-20 [Link]'), (6, 17, '2023-02-23 [Link]'),
(5, 12, '2023-02-24 [Link]'), (1, 1, '2023-02-25 [Link]'), (5, 12, '2023-02-25 [Link]'),
(3, 2, '2023-02-26 [Link]'), (4, 10, '2023-02-27 [Link]'), (7, 18, '2023-03-01 [Link]'),
(6, 22, '2023-03-03 [Link]'), (2, 20, '2023-03-07 [Link]'), (4, 10, '2023-03-08 [Link]'),
(7, 13, '2023-03-11 [Link]'), (1, 23, '2023-03-12 [Link]'), (1, 8, '2023-03-13 [Link]'),
(3, 6, '2023-03-16 [Link]'), (4, 16, '2023-03-17 [Link]'), (5, 18, '2023-03-17 [Link]'),
(7, 2, '2023-03-25 [Link]'), (1, 9, '2023-03-26 [Link]'), (1, 21, '2023-03-30 [Link]'),
(3, 18, '2023-04-06 [Link]'), (5, 18, '2023-04-11 [Link]'), (2, 2, '2023-04-12 [Link]'),
(4, 23, '2023-04-15 [Link]'), (6, 8, '2023-04-16 [Link]'), (1, 1, '2023-04-20 [Link]'),
(1, 3, '2023-04-25 [Link]'), (2, 12, '2023-04-27 [Link]'), (5, 16, '2023-04-28 [Link]'),
(7, 22, '2023-04-29 [Link]'), (1, 23, '2023-04-29 [Link]'), (2, 3, '2023-04-30 [Link]'),
(3, 15, '2023-04-30 [Link]');
14
-- Section 6: Selecting from a Table
-- 6-2 SELECT Statement
/*
*/
USE coffee_store;
15
-- 6-4 Using Inequality Statements
/*
*/
USE coffee_store;
USE coffee_store;
16
-- 6-8 IN and NOT IN
/*
*/
USE coffee_store;
-- 6-9 BETWEEN
USE coffee_store;
17
-- 6-10 LIKE
/*
*/
USE coffee_store;
18
-- 6-11 ORDER BY
/*
*/
USE coffee_store;
# noinspection SqlRedundantOrderingDirection
SELECT * FROM products
ORDER BY price ASC;
19
-- 6-14 DISTINCT
use coffee_store;
20
-- 6-15 LIMIT
/*
*/
USE coffee_store;
USE coffee_store;
21
-- Section 7: Selecting from a Multiple Tables
-- 7-3 INNER JOIN
/*
*/
USE coffee_store;
22
-- 7-4 LEFT JOIN
USE coffee_store;
UPDATE orders
SET customer_id = Null
WHERE id IN (1, 3, 5);
UPDATE orders
SET customer_id = 18
WHERE id = 1;
UPDATE orders
SET customer_id = 12
WHERE id = 3;
UPDATE orders
SET customer_id = 13
WHERE id = 5;
23
-- 7-5 RIGHT JOIN
USE coffee_store;
# Export to left_join.csv
SELECT o.*, c.* FROM customers c
LEFT JOIN orders o ON o.customer_id = [Link]
ORDER BY [Link];
# Export to right_join.csv
SELECT o.*, c.* FROM orders o
RIGHT JOIN customers c ON o.customer_id = [Link]
ORDER BY o.order_time;
/*
*/
USE coffee_store;
24
-- Section 9: Creating a Cinema Booking System
SHOW DATABASES;
SHOW DATABASES;
25
-- 9-3b through 9-9 Create Tables
USE cinema_booking_system;
SHOW TABLES;
DESCRIBE films;
SHOW TABLES;
DESCRIBE customers;
SHOW TABLES;
DESCRIBE rooms;
26
FOREIGN KEY (room_id) REFERENCES rooms(id)
);
SHOW TABLES;
DESCRIBE screenings;
SHOW TABLES;
DESCRIBE seats;
SHOW TABLES;
DESCRIBE bookings;
SHOW TABLES;
DESCRIBE reserved_seat;
27
-- 9-10 Insert the Data
28
('Steve','Shoogan','sshoogan@[Link]'),
('Matthew','Gray','graym@[Link]'),
('Sally','Kramer','kallysramer@[Link]'),
('Peter','Chef','chefp@[Link]'),
(NULL,'Cooke','nullcooke@[Link]'),
('Marty','Seinfeld','marfeld@[Link]'),
('Carl','Green','creen@[Link]'),
('Emma','Greene','egreene@[Link]'),
('Michael','Bannon','mbannon@[Link]'),
('Paul','Showman','theshowman1@[Link]'),
('Harry','Goldthistle','hgold@[Link]'),
('Gill','Newton','gnew@[Link]'),
('Preston','Peters','p2p@[Link]'),
('Simon','Smithson','sismithson@[Link]'),
('Benjamin','Rabbit','brabbit@[Link]'),
('Jerry','David','curb@[Link]'),
('Olivia','Tulip','otulip@[Link]'),
('Richard','Horne','richhorne@[Link]'),
('Betty','Corbe','bcorbe@[Link]'),
('Anne','Callaway','acway@[Link]'),
('Yifan','Way','yway@[Link]'),
('Charlie','Cheene','ccheene@[Link]'),
('Ula','Boon','uboon@[Link]'),
('Buster','David','bdavid@[Link]'),
('Dustin','Beer','beerd@[Link]'),
('John','Sleet','knoweverything@[Link]'),
('August','Summer','asummer@[Link]'),
('Linda','Martins','lmart@[Link]'),
('Chris','Kay','chkay@[Link]'),
('Christopher','Roberts','croberts@[Link]'),
('Robert','Winston','rwin@[Link]'),
('Daniel','Orbe','dorbe@[Link]'),
('Joshua','Wells','wellj@[Link]'),
('Fred','Flinton','ff100@[Link]'),
('Paula','Winner','pwinner@[Link]'),
(NULL,'Canter','canter99@[Link]'),
('Michael','Bassett','mbass@[Link]'),
('Michelle','Martins','mm109@[Link]'),
('Lorna','Brown','lbrown@[Link]'),
('Kerry','Vale','wish1985@[Link]'),
('Sean','Borne','sborne@[Link]'),
('Craig','Evans','cevans@[Link]'),
('Alex','Sanders','asand@[Link]'),
('George','McNabb','gnabb@[Link]'),
('Richard','Trout','redtrout@[Link]'),
('Stewart','Dee','sdee@[Link]'),
('Sue','Werkins','swerkins@[Link]'),
('Mel','Wozniack','mwoz@[Link]'),
('Steve','Dole','sdole@[Link]'),
('Zara','Yelland','zy27@[Link]'),
('Irene','Mellon','mellonirene@[Link]'),
('Karen','Wall','kwall@[Link]'),
('Lucy','Oak','loak@[Link]'),
('Orson','Young','oyoung@[Link]'),
29
('Oliver','Daniels','odaniels@[Link]'),
('Queen','Bee','qbee@[Link]'),
('Vera','Jack','verajack@[Link]'),
('Thomas','Soft','softt@[Link]'),
(NULL,'Blair','nblair@[Link]'),
('Tim','Jones','tjones@[Link]'),
('Nicola','Jenkins','njenk@[Link]'),
('Olivia','Jones','ojo@[Link]'),
('James','Depp','jdepp@[Link]'),
('Meryl','Steep','msteep@[Link]'),
('Jake','Portman','jportman@[Link]'),
('Jenny','Appleton','japple@[Link]'),
('Simon','Hoover','shoover@[Link]'),
('Belinda','Marshall','belindam@[Link]'),
('Stephanie','Watson','swatson@[Link]'),
('Art','Simons','simons@[Link]'),
('Stan','Smith','smithstan@[Link]'),
('Christopher','Roberts','robertsc@[Link]'),
('Nathan','Gold','engold@[Link]'),
('Alain','Bennet','alainb@[Link]'),
('Lewis','Russelton','lrus@[Link]'),
('Josh','Reynolds','jrey@[Link]'),
('William','Williams','williamw@[Link]'),
(NULL,'Pointer','apointer@[Link]'),
('Bort','Skinner','mynameisalsobort@[Link]'),
(NULL,'Smith','lonsmith@[Link]'),
('Susan','Williams','swill@[Link]'),
('Yu','Xi','yuxi@[Link]'),
('Annabelle','Winks','anwinks@[Link]'),
('Andrew','Landhurst','andylandy@[Link]'),
('Winston','Smith','room101@[Link]'),
('Gemma','Arton','garton@[Link]'),
('Renton','McDonald','rent@[Link]'),
('Maggie','Chu','chuchu@[Link]');
30
INSERT INTO screenings (film_id, room_id, start_time)
VALUES (9, 3, '2022-01-01 [Link]'), (3, 2, '2022-01-03 [Link]'), (8, 1, '2022-01-06 [Link]'),
(3, 1, '2022-01-12 [Link]'), (9, 1, '2022-01-12 [Link]'), (8, 2, '2022-01-19 [Link]'),
(8, 2, '2022-01-19 [Link]'), (4, 2, '2022-01-22 [Link]'), (3, 3, '2022-01-22 [Link]'),
(2, 2, '2022-01-22 [Link]'), (6, 3, '2022-01-27 [Link]'), (7, 1, '2022-01-27 [Link]'),
(11,2, '2022-01-28 [Link]'), (6, 2, '2022-01-28 [Link]'), (3, 1, '2022-01-30 [Link]'),
(1, 2, '2022-02-03 [Link]'), (10,1, '2022-02-06 [Link]'), (7, 1, '2022-02-11 [Link]'),
(9, 1, '2022-02-12 [Link]'), (2, 3, '2022-02-14 [Link]'), (6, 2, '2022-02-19 [Link]'),
(9, 2, '2022-02-19 [Link]'), (1, 2, '2022-02-19 [Link]'), (6, 1, '2022-02-20 [Link]'),
(6, 1, '2022-02-22 [Link]'), (6, 2, '2022-02-22 [Link]'), (3, 3, '2022-02-24 [Link]'),
(3, 3, '2022-02-25 [Link]'), (4, 1, '2022-03-06 [Link]'), (4, 1, '2022-03-06 [Link]'),
(2, 2, '2022-03-08 [Link]'), (2, 3, '2022-03-09 [Link]'), (1, 3, '2022-03-10 [Link]'),
(8, 2, '2022-03-11 [Link]'), (4, 2, '2022-03-18 [Link]'), (4, 3, '2022-03-20 [Link]'),
(6, 1, '2022-03-26 [Link]'), (1, 2, '2022-04-01 [Link]'), (4, 1, '2022-04-02 [Link]'),
(3, 2, '2022-04-12 [Link]'), (9, 3, '2022-04-14 [Link]'), (5, 3, '2022-04-17 [Link]'),
(4, 3, '2022-04-20 [Link]'), (7, 1, '2022-04-25 [Link]'), (1, 2, '2022-04-27 [Link]'),
(7, 1, '2022-04-29 [Link]'), (2, 3, '2022-05-01 [Link]'), (7, 2, '2022-05-01 [Link]'),
(5, 2, '2022-05-11 [Link]'), (5, 1, '2022-05-14 [Link]'), (8, 3, '2022-05-14 [Link]'),
(8, 2, '2022-05-15 [Link]'), (10,1, '2022-05-15 [Link]'), (5, 3, '2022-05-16 [Link]'),
(6, 2, '2022-05-16 [Link]'), (6, 2, '2022-05-18 [Link]'), (1, 2, '2022-05-25 [Link]'),
(11,1, '2022-06-07 [Link]'), (1, 1, '2022-06-08 [Link]'), (2, 2, '2022-06-14 [Link]'),
(2, 2, '2022-06-17 [Link]'), (5, 2, '2022-06-18 [Link]'), (10,3, '2022-06-18 [Link]'),
(9, 2, '2022-06-18 [Link]'), (8, 1, '2022-06-26 [Link]'), (2, 2, '2022-06-28 [Link]'),
(2, 3, '2022-06-30 [Link]'), (11,2, '2022-07-02 [Link]'), (10,3, '2022-07-03 [Link]'),
(10,1, '2022-07-06 [Link]'), (3, 2, '2022-07-09 [Link]'), (4, 3, '2022-07-12 [Link]'),
(5, 3, '2022-07-13 [Link]'), (2, 2, '2022-07-29 [Link]'), (11,2, '2022-08-03 [Link]'),
(6, 1, '2022-08-05 [Link]'), (6, 1, '2022-08-06 [Link]'), (11,1, '2022-08-08 [Link]'),
(9, 1, '2022-08-11 [Link]'), (6, 2, '2022-08-12 [Link]'), (9, 1, '2022-08-16 [Link]'),
(6, 2, '2022-08-20 [Link]'), (4, 2, '2022-08-24 [Link]'), (5, 1, '2022-08-26 [Link]'),
(7, 2, '2022-09-02 [Link]'), (2, 3, '2022-09-02 [Link]'), (9, 3, '2022-09-05 [Link]'),
(2, 3, '2022-09-07 [Link]'), (4, 2, '2022-09-14 [Link]'), (1, 3, '2022-09-17 [Link]'),
(4, 3, '2022-09-17 [Link]'), (11,3, '2022-09-18 [Link]'), (6, 2, '2022-09-21 [Link]'),
(5, 1, '2022-09-30 [Link]'), (4, 2, '2022-10-01 [Link]'), (9, 2, '2022-10-09 [Link]'),
(5, 3, '2022-10-12 [Link]'), (1, 3, '2022-10-13 [Link]'), (6, 1, '2022-10-15 [Link]'),
(2, 3, '2022-10-18 [Link]'), (1, 2, '2022-10-22 [Link]'), (7, 2, '2022-10-25 [Link]'),
(2, 3, '2022-10-26 [Link]'), (5, 2, '2022-10-27 [Link]'), (3, 3, '2022-11-06 [Link]'),
(10,3, '2022-11-17 [Link]'), (7, 1, '2022-11-22 [Link]'), (11,2, '2022-11-25 [Link]'),
(3, 1, '2022-11-26 [Link]'), (10,1, '2022-11-26 [Link]'), (5, 3, '2022-11-28 [Link]'),
(7, 2, '2022-11-29 [Link]'), (8, 2, '2022-12-02 [Link]'), (10,2, '2022-12-08 [Link]'),
(7, 2, '2022-12-09 [Link]'), (11,3, '2022-12-10 [Link]'), (2, 1, '2022-12-13 [Link]'),
(6, 1, '2022-12-15 [Link]'), (11,1, '2022-12-21 [Link]'), (8, 2, '2022-12-22 [Link]'),
(6, 3, '2022-12-24 [Link]'), (8, 2, '2022-12-26 [Link]'), (3, 3, '2022-12-29 [Link]'),
(6, 2, '2023-01-01 [Link]'), (6, 1, '2023-01-04 [Link]'), (6, 1, '2023-01-06 [Link]'),
(10,3, '2023-01-07 [Link]'), (10,1, '2023-01-08 [Link]'), (3, 2, '2023-01-10 [Link]'),
(4, 1, '2023-01-16 [Link]'), (11,1, '2023-01-16 [Link]'), (10,2, '2023-01-22 [Link]'),
(11,2, '2023-01-25 [Link]'), (6, 3, '2023-01-27 [Link]'), (3, 1, '2023-01-31 [Link]'),
(3, 1, '2023-01-31 [Link]'), (2, 1, '2023-02-02 [Link]'), (3, 1, '2023-02-08 [Link]'),
(8, 1, '2023-02-08 [Link]'), (4, 3, '2023-02-11 [Link]'), (1, 3, '2023-02-15 [Link]'),
(1, 2, '2023-02-16 [Link]'), (2, 3, '2023-02-20 [Link]'), (5, 2, '2023-02-25 [Link]'),
(9, 3, '2023-02-26 [Link]'), (2, 2, '2023-03-02 [Link]'), (7, 3, '2023-03-07 [Link]'),
(11,2, '2023-03-12 [Link]'), (5, 1, '2023-03-13 [Link]'), (8, 1, '2023-03-15 [Link]'),
(5, 3, '2023-03-20 [Link]'), (10,1, '2023-03-20 [Link]'), (1, 1, '2023-03-24 [Link]'),
(2, 2, '2023-03-26 [Link]'), (2, 1, '2023-03-28 [Link]'), (4, 2, '2023-03-31 [Link]'),
(9, 1, '2023-03-31 [Link]'), (10,2, '2023-04-08 [Link]'), (3, 3, '2023-04-12 [Link]'),
(9, 3, '2023-04-13 [Link]'), (9, 3, '2023-04-14 [Link]'), (3, 1, '2023-04-15 [Link]'),
(9, 3, '2023-04-15 [Link]'), (2, 2, '2023-04-19 [Link]'), (6, 3, '2023-04-21 [Link]'),
(4, 2, '2023-04-25 [Link]'), (1, 3, '2023-04-27 [Link]'), (3, 1, '2023-04-27 [Link]'),
(8, 2, '2023-05-02 [Link]'), (11,2, '2023-05-11 [Link]'), (8, 3, '2023-05-14 [Link]'),
(2, 1, '2023-05-23 [Link]'), (11,1, '2023-05-25 [Link]'), (7, 2, '2023-06-05 [Link]'),
(2, 3, '2023-06-08 [Link]'), (7, 1, '2023-06-09 [Link]'), (7, 2, '2023-06-14 [Link]'),
(1, 2, '2023-06-16 [Link]'), (8, 3, '2023-06-16 [Link]'), (11,2, '2023-06-19 [Link]'),
31
(11,2, '2023-06-19 [Link]'), (6, 3, '2023-06-25 [Link]'), (3, 1, '2023-06-26 [Link]'),
(1, 3, '2023-06-30 [Link]'), (6, 2, '2023-07-06 [Link]'), (1, 1, '2023-07-08 [Link]'),
(10,2, '2023-07-08 [Link]'), (2, 2, '2023-07-08 [Link]'), (10,1, '2023-07-09 [Link]'),
(10,3, '2023-07-15 [Link]'), (1, 3, '2023-07-18 [Link]'), (3, 1, '2023-07-22 [Link]'),
(4, 3, '2023-07-27 [Link]'), (9, 3, '2023-07-28 [Link]'), (10,3, '2023-07-29 [Link]'),
(4, 3, '2023-07-30 [Link]'), (5, 1, '2023-08-08 [Link]'), (5, 2, '2023-08-12 [Link]'),
(5, 1, '2023-08-15 [Link]'), (11,2, '2023-08-21 [Link]'), (10,2, '2023-08-23 [Link]'),
(3, 2, '2023-08-25 [Link]'), (1, 3, '2023-08-27 [Link]'), (2, 1, '2023-08-28 [Link]'),
(8, 1, '2023-08-29 [Link]'), (9, 2, '2023-08-30 [Link]'), (9, 2, '2023-08-30 [Link]'),
(7, 2, '2023-08-31 [Link]'), (8, 1, '2023-09-01 [Link]'), (1, 2, '2023-09-02 [Link]'),
(9, 3, '2023-09-03 [Link]'), (9, 3, '2023-09-07 [Link]'), (10,2, '2023-09-09 [Link]'),
(3, 1, '2023-09-09 [Link]'), (11,3, '2023-09-10 [Link]'), (3, 1, '2023-09-11 [Link]'),
(10,3, '2023-09-13 [Link]'), (11,1, '2023-09-14 [Link]'), (5, 1, '2023-09-22 [Link]'),
(9, 2, '2023-09-30 [Link]');
32
(86, 80 ), (45, 19 ), (50, 44 ), (88, 117), (104,61 ), (100,97 ), (81, 35 ), (85, 3 ),
(89, 35 ), (76, 41 ), (86, 90 ), (48, 17 ), (53, 33 ), (94, 116), (110,4 ), (106,54 ),
(84, 33 ), (88, 2 ), (95, 34 ), (76, 27 ), (86, 29 ), (51, 28 ), (56, 39 ), (97, 115),
(116,4 ), (112,32 ), (90, 33 ), (88, 1 ), (101,34 ), (82, 90 ), (86, 101), (51, 104),
(59, 111), (100,114), (122,7 ), (118,35 ), (93, 57 ), (88, 77 ), (107,69 ), (91, 96 ),
(92, 4 ), (54, 35 ), (92, 100), (106,113), (128,9 ), (124,60 ), (87, 88 ), (91, 39 ),
(113,67 ), (91, 43 ), (98, 90 ), (60, 36 ), (98, 99 ), (112,112), (134,10 ), (130,43 ),
(69, 84 ), (97, 31 ), (119,1 ), (97, 3 ), (104,7 ), (63, 1 ), (104,4 ), (118,111),
(140,1 ), (136,22 ), (72, 67 ), (97, 111), (125,87 ), (100,24 ), (110,46 ), (66, 4 ),
(110,17 ), (130,110), (146,12 ), (142,22 ), (75, 48 ), (100,111), (131,47 ), (103,24 ),
(122,27 ), (69, 27 ), (122,55 ), (136,91 ), (152,11 ), (148,13 ), (84, 90 ), (103,101),
(137,12 ), (109,11 ), (128,10 ), (75, 9 ), (128,58 ), (142,90 ), (158,56 ), (154,15 ),
(93, 91 ), (109,34 ), (143,9 ), (115,34 ), (131,33 ), (78, 81 ), (134,88 ), (148,48 ),
(164,78 ), (160,16 ), (102,22 ), (115,33 ), (149,47 ), (121,46 ), (137,6 ), (90, 6 ),
(140,67 ), (154,40 ), (170,77 ), (166,78 ), (108,21 ), (121,26 ), (155,53 ), (127,42 ),
(143,31 ), (99, 11 ), (152,45 ), (160,70 ), (176,38 ), (172,66 ), (114,20 ), (127,106),
(161,111), (211,109), (149,108), (102,113), (158,44 ), (166,87 ), (182,99 ), (178,65 ),
(120,19 ), (136,99 ), (167,49 ), (133,66 ), (158,67 ), (117,3 ), (164,71 ), (172,33 ),
(188,100), (184,50 ), (144,18 ), (142,23 ), (173,46 ), (139,23 ), (164,1 ), (129,3 ),
(170,78 ), (178,35 ), (194,36 ), (187,23 ), (159,24 ), (151,23 ), (179,55 ), (145,58 ),
(170,60 ), (141,4 ), (176,70 ), (184,7 ), (200,36 ), (190,78 ), (162,70 ), (157,80 ),
(182,99 ), (151,95 ), (176,32 ), (156,7 ), (182,88 ), (190,8 ), (200,34 ), (193,74 ),
(168,35 ), (163,120), (185,109), (157,31 ), (182,68 ), (180,56 ), (191,47 ), (190,16 ),
(200,1 ), (196,72 ), (177,11 ), (169,12 ), (191,19 ), (166,108), (176,23 ), (189,23 ),
(197,32 ), (196,26 ), (203,2 ), (199,80 ), (192,1 ), (175,49 ), (197,55 ), (172,76 ),
(188,99 ), (195,90 ), (200,23 ), (199,43 ), (206,4 ), (202,90 ), (198,59 ), (181,58 ),
(200,59 ), (178,6 ), (194,35 ), (198,44 ), (203,116), (202,28 ), (209,4 ), (205,93 ),
(201,23 ), (187,10 ), (203,76 ), (187,77 ), (200,78 ), (204,9 ), (209,18 ), (205,10 ),
(209,5 ), (208,43 ), (204,67 ), (193,54 ), (206,54 ), (196,1 ), (206,7 ), (207,9 ),
(212,68 ), (211,5 ), (212,6 ), (211,7 ), (207,8 ), (199,9 ), (209,9 ), (205,23 ),
(212,111), (213,40 );
33
(96, 60 ), (96, 61 ), (97, 81 ), (97, 82 ), (98, 47 ), (98, 48 ), (99, 85 ), (99, 86 ),
(100,118), (100,119), (101,103), (101,104), (102,23 ), (102,24 ), (103,81 ), (103,82 ),
(104,66 ), (104,67 ), (105,136), (105,137), (106,39 ), (106,40 ), (107,75 ), (108,72 ),
(109,101), (109,102), (109,103), (109,104), (109,105), (110,122), (110,123), (110,124),
(111,77 ), (111,78 ), (112,8 ), (113,82 ), (113,83 ), (114,56 ), (114,57 ), (115,139),
(115,140), (116,69 ), (117,75 ), (117,76 ), (117,77 ), (118,60 ), (119,88 ), (120,111),
(121,108), (122,31 ), (122,32 ), (123,98 ), (124,18 ), (124,19 ), (125,120), (125,121),
(126,61 ), (126,62 ), (127,101), (127,102), (128,70 ), (128,71 ), (129,105), (129,106),
(130,141), (130,142), (131,77 ), (132,71 ), (133,106), (133,107), (133,108), (134,61 ),
(135,110), (135,111), (136,52 ), (137,84 ), (137,85 ), (138,37 ), (139,75 ), (139,76 ),
(140,140), (140,141), (141,90 ), (142,42 ), (143,94 ), (143,95 ), (144,66 ), (145,112),
(146,21 ), (147,79 ), (147,80 ), (147,81 ), (148,63 ), (149,89 ), (150,128), (150,129),
(151,77 ), (151,78 ), (151,79 ), (152,57 ), (152,58 ), (152,59 ), (153,89 ), (153,90 ),
(153,91 ), (154,37 ), (154,38 ), (155,111), (155,112), (155,113), (156,19 ), (156,20 ),
(157,83 ), (158,5 ), (159,77 ), (160,140), (161,89 ), (162,9 ), (163,104), (164,71 ),
(165,117), (165,118), (166,66 ), (166,67 ), (167,83 ), (167,84 ), (167,85 ), (167,86 ),
(168,55 ), (168,56 ), (168,57 ), (168,58 ), (169,105), (169,106), (170,121), (170,122),
(171,92 ), (172,36 ), (173,99 ), (174,38 ), (175,117), (175,118), (176,68 ), (176,69 ),
(177,81 ), (177,82 ), (178,31 ), (178,32 ), (179,103), (179,104), (180,119), (181,102),
(182,71 ), (182,72 ), (183,100), (183,101), (184,48 ), (184,49 ), (185,110), (185,111),
(186,12 ), (186,13 ), (187,78 ), (187,79 ), (188,39 ), (189,81 ), (190,135), (191,79 ),
(192,33 ), (192,34 ), (192,35 ), (193,90 ), (194,54 ), (194,55 ), (195,121), (195,122),
(196,53 ), (196,54 ), (197,80 ), (197,81 ), (198,2 ), (198,3 ), (199,81 ), (199,82 ),
(200,115), (201,76 ), (202,27 ), (203,103), (203,104), (204,22 ), (205,140), (206,51 ),
(207,83 ), (208,64 ), (193,91 ), (210,118), (211,101), (212,61 ), (212,62 ), (213,79 ),
(213,80 ), (214,50 ), (215,111), (216,30 ), (216,31 ), (217,106), (217,107), (218,40 ),
(218,41 ), (219,91 ), (219,92 ), (220,123), (221,76 ), (222,20 ), (222,21 ), (223,105),
(224,60 ), (225,133), (225,134), (226,70 ), (226,71 ), (226,72 ), (227,83 ), (228,47 ),
(229,80 ), (230,120), (230,121), (231,85 ), (231,86 ), (232,11 ), (232,12 ), (232,13 ),
(233,81 ), (233,82 ), (234,62 ), (234,63 ), (235,113), (235,114), (236,35 ), (236,36 ),
(237,81 ), (237,82 ), (238,41 ), (238,42 ), (239,77 ), (239,78 ), (383,78 ), (241,102),
(241,103), (242,21 ), (242,22 ), (242,23 ), (243,89 ), (244,45 ), (245,137), (245,138),
(245,139), (245,140), (246,49 ), (246,50 ), (246,51 ), (247,101), (248,34 ), (249,77 ),
(250,138), (251,102), (251,103), (251,104), (252,61 ), (253,98 ), (253,99 ), (254,70 ),
(255,119), (255,120), (255,121), (256,23 ), (257,87 ), (258,36 ), (258,37 ), (259,95 ),
(259,96 ), (260,132), (261,88 ), (261,89 ), (261,90 ), (262,20 ), (262,21 ), (262,22 ),
(262,23 ), (263,102), (264,63 ), (265,118), (266,24 ), (267,79 ), (268,68 ), (269,90 ),
(269,91 ), (270,120), (271,104), (272,8 ), (273,76 ), (274,33 ), (275,122), (275,123),
(275,124), (276,11 ), (276,12 ), (276,13 ), (277,82 ), (277,83 ), (278,27 ), (278,28 ),
(279,75 ), (279,76 ), (279,77 ), (280,140), (280,141), (281,80 ), (282,26 ), (283,100),
(283,101), (283,102), (284,23 ), (284,24 ), (285,121), (286,68 ), (287,78 ), (288,4 ),
(288,5 ), (289,97 ), (290,113), (290,114), (291,81 ), (291,82 ), (292,49 ), (293,85 ),
(294,39 ), (295,140), (296,68 ), (297,82 ), (298,25 ), (298,26 ), (299,83 ), (299,84 ),
(300,134), (300,135), (301,101), (301,102), (302,56 ), (302,57 ), (303,92 ), (303,93 ),
(304,5 ), (305,112), (305,113), (306,61 ), (306,62 ), (307,81 ), (308,72 ), (309,88 ),
(309,89 ), (228,48 ), (311,97 ), (311,98 ), (311,99 ), (312,39 ), (312,40 ), (313,79 ),
(314,61 ), (314,62 ), (315,144), (316,55 ), (317,81 ), (317,82 ), (318,66 ), (319,101),
(365,122), (321,83 ), (321,84 ), (322,13 ), (322,14 ), (322,15 ), (323,89 ), (323,90 ),
(324,37 ), (324,38 ), (325,115), (325,116), (326,68 ), (326,69 ), (326,70 ), (327,108),
(328,61 ), (329,77 ), (330,125), (330,126), (331,91 ), (331,92 ), (332,58 ), (332,59 ),
(333,76 ), (333,77 ), (334,23 ), (334,24 ), (335,131), (335,132), (336,10 ), (336,11 ),
(337,99 ), (337,100), (338,67 ), (338,68 ), (339,101), (339,102), (340,138), (341,105),
(342,3 ), (342,4 ), (343,74 ), (343,75 ), (344,58 ), (344,59 ), (345,122), (345,123),
(346,34 ), (346,35 ), (347,79 ), (347,80 ), (348,27 ), (348,28 ), (348,29 ), (349,77 ),
(350,112), (350,113), (351,83 ), (351,84 ), (352,67 ), (352,68 ), (352,69 ), (353,101),
(353,102), (353,103), (354,13 ), (355,113), (355,114), (356,45 ), (357,90 ), (358,70 ),
(359,79 ), (360,138), (361,97 ), (362,72 ), (363,80 ), (364,54 ), (365,122), (366,30 ),
(367,89 ), (368,48 ), (369,103), (370,110), (371,88 ), (372,62 ), (373,81 ), (373,82 ),
(374,59 ), (375,140), (376,70 ), (377,93 ), (378,23 ), (378,24 ), (378,25 ), (378,26 ),
(379,79 ), (379,80 ), (379,81 ), (380,118), (380,119), (381,103), (382,66 ), (382,67 ),
(382,68 ), (382,69 ), (383,77 ), (384,41 ), (384,42 ), (384,43 ), (385,125), (386,68 ),
34
(387,84 ), (388,70 ), (388,71 ), (389,95 ), (390,120), (390,121), (390,122), (391,76 ),
(391,77 ), (391,78 ), (392,8 ), (392,9 ), (392,10 ), (393,103), (394,39 ), (394,40 ),
(395,127), (395,128), (395,129), (396,66 ), (396,67 ), (397,85 ), (397,86 ), (398,58 ),
(399,83 ), (399,84 ), (400,118), (400,119), (400,120), (401,103), (401,104), (401,105),
(402,51 ), (403,75 ), (404,34 ), (404,35 ), (405,109), (405,110), (405,111), (406,72 ),
(407,93 ), (407,94 ), (408,49 ), (409,88 ), (410,117), (410,118), (410,119);
35
-- Section 10: Aggregate Functions
-- 10-3 COUNT
USE cinema_booking_system;
-- 10-4 SUM
USE cinema_booking_system;
36
-- 10-4 MIN and MAX
USE cinema_booking_system;
-- 10-6 AVERAGE
USE cinema_booking_system;
37
-- 10-9 Grouping Data
USE cinema_booking_system;
USE cinema_booking_system;
# noinspection SqlAggregates
SELECT customer_id, screening_id, count(*) FROM bookings
GROUP BY customer_id, screening_id
# WHERE customer_id = 10; # will NOT work.
HAVING customer_id = 10;
# noinspection SqlAggregates
SELECT customer_id, screening_id, count(*) FROM bookings
JOIN screenings on bookings.screening_id = [Link]
WHERE year(screenings.start_time) = 2022
GROUP BY customer_id, screening_id
HAVING customer_id = 10;
38
-- Section 11: Aggregate Functions
-- 11-3 Non-Correlated Subqueries Part 1
USE cinema_booking_system;
USE cinema_booking_system;
39
-- 11-5 Correlated Subqueries
USE cinema_booking_system;
# This inner query will not run without outer query above
# defining bookings b.
# This is a Non-Correlated query.
SELECT count(*)
FROM reserved_seat WHERE booking_id = [Link];
40
-- Section 12: MySQL String and Date Functions
-- 12-3 Concatenation
-- CONCATENATE
USE cinema_booking_system;
SELECT concat("This is ", first_name, " ", last_name, "'s email address: ",
email)
AS customer_email FROM customers;
-- 12-4 Substrings
-- SUBSTRING
USE cinema_booking_system;
41
-- 12-5 Upper and Lower Case
-- Upper and Lower Case
USE cinema_booking_system;
USE cinema_booking_system;
42
-- 12-9 Month Function
USE cinema_booking_system;
# Returns 9
SELECT month('2023-09-22 [Link]');
USE cinema_booking_system;
43