0% found this document useful (0 votes)
9 views145 pages

LPA SQL for Beginners Learn SQL Using MySQL and Database Design SEP2024

The document provides an overview of the SQL for Beginners course offered on Udemy, including course objectives, syllabus, and key topics such as database design, SQL queries, and data manipulation. It is designed for individuals with no prior knowledge of SQL and aims to help them become proficient in using MySQL. Students can access the course materials for free and are encouraged to provide feedback through the Q&A section.

Uploaded by

debidattaray
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)
9 views145 pages

LPA SQL for Beginners Learn SQL Using MySQL and Database Design SEP2024

The document provides an overview of the SQL for Beginners course offered on Udemy, including course objectives, syllabus, and key topics such as database design, SQL queries, and data manipulation. It is designed for individuals with no prior knowledge of SQL and aims to help them become proficient in using MySQL. Students can access the course materials for free and are encouraged to provide feedback through the Q&A section.

Uploaded by

debidattaray
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
You are on page 1/ 145

These are the slides for the SQL for Beginners: Learn SQL using MySQL and Database

Design on Udemy. They are


provided free of charge to all students.

More information about the course: https://lpa.dev/u1sqlb

If you have any questions of queries, please add your feedback in the Q&A section of the course on Udemy.

Best regards,

Tim Buchalka
Learn Programming Academy

SQL FOR BEGINNERS SQL FOR BEGINNERS


Slides September, 2024
SQL for Beginners Slides

Main Course Slides.

SQL FOR BEGINNERS


SQL for Beginners Slides
SQL FOR BEGINNERS

IT Systems Engineer

COURSE INTRODUCTION
SQL-0020-1
COURSE INTRODUCTION

SQL FOR BEGINNERS COURSE INTRODUCTION


Course Introduction SQL-0020-2
No Prior Knowledge

No Prior Knowledge

Exper
Beginner
t

SQL FOR BEGINNERS COURSE INTRODUCTION


Course Introduction SQL-0020-3
In The Course
Database Design

Creating a Real World Database from Scratch

Writing Complex Queries To Retrieve Data

Challenges For You to Test Your Knowledge

SQL FOR BEGINNERS COURSE INTRODUCTION


Course Introduction SQL-0020-4
This Course Is For
You
You are new to SQL and want to become an expert on SQL and
databases

You are new to MySQL and want to master a second database

You know the basics of SQL and want to take your skills to the
next level

SQL FOR BEGINNERS COURSE INTRODUCTION


Course Introduction SQL-0020-5
COURSE INTRODUCTION

SQL FOR BEGINNERS COURSE INTRODUCTION


Course Introduction SQL-0020-6
SYLLABUS OVERVIEW

COURSE INTRODUCTION
SQL-0040-1
Section 2
Learn about Databases, SQL and MySQL

Install and Setup MySQL and MySQL Workbench

Write Your First SQL queries.

SQL FOR BEGINNERS COURSE INTRODUCTION


SYLLABUS OVERVIEW SQL-0040-2
Section 3
Primary and Foreign Keys

How to Create, Modifying and Delete Tables

Creating a Database for a Coffee Store

SQL FOR BEGINNERS COURSE INTRODUCTION


SYLLABUS OVERVIEW SQL-0040-3
Section 4
How to Insert, Update and Delete Data

Insert Data into the Coffee Store Database

SQL FOR BEGINNERS COURSE INTRODUCTION


SYLLABUS OVERVIEW SQL-0040-4
Section 5
How to Retrieve Data from a Table

Filter using Where Clauses

Ordering Your Data

SQL FOR BEGINNERS COURSE INTRODUCTION


SYLLABUS OVERVIEW SQL-0040-5
Section 6
Retrieve Data from More Than One Table

How to Use Table Joins

Differences Between Joins

SQL FOR BEGINNERS COURSE INTRODUCTION


SYLLABUS OVERVIEW SQL-0040-6
Section 7
Learn About Database Design

Normalization

Relationships

Constraints

SQL FOR BEGINNERS COURSE INTRODUCTION


SYLLABUS OVERVIEW SQL-0040-7
Section 8
Create a Database for a Cinema Online Booking System

Insert Data into this Database

SQL FOR BEGINNERS COURSE INTRODUCTION


SYLLABUS OVERVIEW SQL-0040-8
Section 9
Learn About Aggregate Functions

How to Group Data

Use the Having Clause

SQL FOR BEGINNERS COURSE INTRODUCTION


SYLLABUS OVERVIEW SQL-0040-9
Section 10
Learn About Subqueries

Non-Correlated Subqueries

Correlated Subqueries

SQL FOR BEGINNERS COURSE INTRODUCTION


SYLLABUS OVERVIEW SQL-0040-10
WHAT YOU WILL LEARN
What a database is.

What SQL and MySQL are and how they are related.

What a relational Database Management System is.

Installation and setup of MySQL and MySQL Workbench.

Writing your first SQL queries.

SQL FOR BEGINNERS INTRODUCTION TO DATABASES


Section Introduction SQL-0060-1
DATABASES, SQL AND MySQL

INTRODUCTION TO DATABASES
SQL-0080-1
WHAT IS A DATABASE
An organised collection of information (data).

SQL FOR BEGINNERS INTRODUCTION TO DATABASES


Databases, SQL AND MySQL SQL-0080-2
WHAT IS A DATABASE
Allows us to access and interact with the data.

SQL SQL

SQL SQL

SQL FOR BEGINNERS INTRODUCTION TO DATABASES


Databases, SQL AND MySQL SQL-0080-3
WHAT IS SQL
Structured Query Language.

It is a standard language used to communicate with


databases.

SQL is used to perform tasks on a database.

SELECT * FROM
customers;

SQL FOR BEGINNERS INTRODUCTION TO DATABASES


Databases, SQL AND MySQL SQL-0080-4
WHAT IS MySQL
MySQL is a Relational Database Management System.

Provides a UI for us to access and interact with the database.

RDBMS
MySQL
PostgreSQL
SQL Server
Oracle

SQL FOR BEGINNERS INTRODUCTION TO DATABASES


Databases, SQL AND MySQL SQL-0080-5
RELATIONAL DATABASE
MANAGEMENT SYSTEM
RDBMS

INTRODUCTION TO DATABASES
SQL-0090-1
WHAT IS AN RDBMS
A relational database is a collection of data organised into
tables.

Tables contain columns of data categories, and rows with


particular instances of that data category.

SQL FOR BEGINNERS INTRODUCTION TO DATABASES


RDBMS SQL-0090-2
WHAT IS AN RDBMS
Tables in a relational database can be linked together.

RDBMS is what we use to access and interact with the relational


database.
SQL FOR BEGINNERS INTRODUCTION TO DATABASES
RDBMS SQL-0090-3
SQL FOR BEGINNERS
Data Definition Language
Section Introduction

DATA DEFINITION LANGUAGE


SQL-0180-1
BY THE END OF THIS SECTION
DATA TYPES

PRIMARY AND FOREIGN KEYS

CREATING TABLES

MODIFYING TABLES

DELETING TABLES

DELETING ALL DATA FROM TABLES

SQL FOR BEGINNERS DATA DEFINITION LANGUAGE


Section Introduction SQL-0180-2
CREATING YOUR FIRST DATABASE
CREATE A DATABASE FOR A COFFEE SHOP

PRODUCTS

CUSTOMERS

ORDERS

SQL FOR BEGINNERS DATA DEFINITION LANGUAGE


Section Introduction SQL-0180-3
SQL FOR BEGINNERS
Data Types

DATA DEFINITION LANGUAGE


SQL-0200-1
NUMERIC DATA TYPES
INT : Whole numbers

FLOAT(M,D) : Decimal numbers (approximate)

DECIMAL(M,D) : Decimal numbers (precise)

SQL FOR BEGINNERS DATA DEFINITION LANGUAGE


Data Types SQL-0200-2
NON-NUMERIC DATA TYPES
CHAR(N) : Fixed length character

VARCHAR(N) : Varying length character

ENUM(‘M’,’F’) : Value from a defined list

BOOLEAN : True of False values

SQL FOR BEGINNERS DATA DEFINITION LANGUAGE


Data Types SQL-0200-3
DATE AND TIME TYPES
DATE : Date (YYYY-MM-DD)

DATETIME : Date and the time (YYYY-MM-DD HH-MM-SS)

TIME : Time (HHH-MM-SS)

YEAR : Year (YYYY)

SQL FOR BEGINNERS DATA DEFINITION LANGUAGE


Data Types SQL-0200-4
SQL FOR BEGINNERS
Primary and Foreign Keys

DATA DEFINITION LANGUAGE


SQL-0220-1
PRIMARY KEY
•A primary key is a column, or set of columns, which uniquely identifies a record within a
table.

•A primary key must be unique.

•A primary key cannot be NULL.

•A table can only have one primary key.

When we create our tables we will learn how to define our primary keys.

SQL FOR BEGINNERS DATA DEFINITION LANGUAGE


Primary and Foreign Keys SQL-0220-2
PRIMARY KEY EXAMPLE

The ID column is the PRIMARY KEY in the customers table.

Can’t be the first_name, last_name or even telephone number as they are not unique.

SQL FOR BEGINNERS DATA DEFINITION LANGUAGE


Primary and Foreign Keys SQL-0220-3
FOREIGN KEY
•A foreign key is used to link two tables together.

•A foreign key is a column whose values match the values of another tables primary key
column.

• The table with the primary key is called the reference, or parent, table and the table with
the foreign key is called the child table.

•A table can have multiple foreign keys.

SQL FOR BEGINNERS DATA DEFINITION LANGUAGE


Primary and Foreign Keys SQL-0220-4
FOREIGN KEY EXAMPLE

Customer_id column and product_id column are FOREIGN KEYS in the orders table.

They are referencing PRIMARY KEY columns in the customers and products tables.

SQL FOR BEGINNERS DATA DEFINITION LANGUAGE


Primary and Foreign Keys SQL-0220-5
Creating the Coffee Store Database

CUSTOMERS
ID FIRST_NAME LAST_NAME GENDER PHONE_NUMBER

SQL FOR BEGINNERS DATA DEFINITION LANGUAGE


Creating the Coffee Store Database SQL-0240-1
MORE ON ALTER TABLE
SECTION INTRODUCTION

MORE ON ALTER TABLE


SQL-0302-1
BY THE END OF THIS SECTION
HOW TO ADD AND REMOVE PRIMARY KEYS

HOW TO ADD AND REMOVE FOREIGN KEYS

HOW TO ADD A UNIQUE CONSTRAINT

HOW TO CHANGE A COLUMNS NAME

HOW TO CHANGE A COLUMNS DATA TYPE

SQL FOR BEGINNERS MORE ON ALTER TABLE


Section Introduction SQL-0302-2
IN THIS SECTION WE WILL
CREATE A NEW DATABASE

MODIFY THE TABLES WITHIN THIS DATABASE

EXERCISES AND SOLUTIONS

SQL FOR BEGINNERS MORE ON ALTER TABLE


Section Introduction SQL-0302-3
Exercise 1
•Add a primary key to the id fields in the pets and people tables.

•Add a foreign key to the owner_id field in the pets table referencing the id
field in the people table.

•Add a column named email to the people table.

•Add a unique constraint to the email column in the people table.

•Rename the name column in the pets table to ‘first_name’.

•Change the postcode data type to CHAR(7) in the addresses table.

SQL FOR BEGINNERS MORE ON ALTER TABLE


Exercise 1 SQL-0316-1
SQL FOR BEGINNERS
Data Manipulation Language
Section Introduction

DATA MANIPULATION LANGUAGE


SQL-0320-1
BY THE END OF THIS SECTION
INSERT DATA INTO TABLES

UPDATE DATA IN A TABLE

DELETE DATA FROM A TABLE

SQL FOR BEGINNERS DATA MANIPULATION LANGUAGE


Section Introduction SQL-0320-2
Insert Into

PRODUCTS
ID NAME PRICE COFFEE_ORIGIN
1 ESPRESSO 2.50 BRAZIL
2 MACCHIATO 3.00 BRAZIL
3 CAPPUCCINO 3.50 COSTA RICA
4 LATTE 3.50 INDONESIA
5 AMERICANO 3.00 BRAZIL
6 FLAT WHITE 3.50 INDONESIA
7 FILTER 3.00 INDIA

SQL FOR BEGINNERS DATA MANIPULATION LANGUAGE


Insert Into SQL-0340-1
SQL FOR BEGINNERS
Selecting from a Table
Section Introduction

SELECTING FROM A TABLE


SQL-0420-1
BY THE END OF THIS SECTION
How to retrieve data from a table using the SELECT command.

Adding WHERE clauses to filter your query.

How to use the IN, BETWEEN and LIKE commands.

How to order your result set by column.

SQL FOR BEGINNERS SELECTING FROM A TABLE


Section Introduction SQL-0420-2
BY THE END OF THIS SECTION
How to limit the number of rows in your extraction.

How to remove duplicate values.

How to set a column name alias.

Exercises to practice your SQL skills.

SQL FOR BEGINNERS SELECTING FROM A TABLE


Section Introduction SQL-0420-3
Exercise 1
1. From the customers table, select the first name and phone number of all the females
who have a last name of Bluth.

2. From the products table, select the name for all products that have a price greater than
3.00 or a coffee origin of Sri Lanka.

3. How many male customers don’t have a phone number entered into the customers
table?

SQL FOR BEGINNERS SELECTING FROM A TABLE


Exercise 1 SQL-0520-1
Exercise 2
1. From the products table, select the name and price of all products with a coffee origin
equal to Colombia or Indonesia. Ordered by name from A-Z.

2. From the orders table, select all the orders from February 2017 for customers with id’s
of 2, 4, 6 or 8.

3. From the customers table, select the first name and phone number of all customers
who’s last name contains the pattern ‘ar’.

SQL FOR BEGINNERS SELECTING FROM A TABLE


Exercise 2 SQL-0640-1
Exercise 3
1. From the customers table, select distinct last names and order alphabetically from A-Z.

2. From the orders table, select the first 3 orders placed by customer with id 1, in February
2017.

3. From the products table, select the name, price and coffee origin but rename the price
to retail_price in the results set.

SQL FOR BEGINNERS SELECTING FROM A TABLE


Exercise 3 SQL-0740-1
SQL FOR BEGINNERS
Selecting from Multiple Tables
Section Introduction

SELECTING FROM MULTIPLE TABLES


SQL-0780-1
BY THE END OF THIS SECTION
Learn how to SELECT data from multiple tables by using different types of JOIN
statements in SQL.

How to write an INNER JOIN query.

How to write LEFT and RIGHT JOINs queries and the differences between them.

How to SELECT data from more than two tables using multiple JOIN statements.

SQL FOR BEGINNERS SELECTING FROM MULTIPLE TABLES


Section Introduction SQL-0780-2
SQL FOR BEGINNERS
Joins

SELECTING FROM MULTIPLE TABLES


SQL-0800-1
WHAT IS A JOIN
Joins allow you to retrieve data from multiple tables in a single select statement.

To join two tables there needs to be a related column between them.

There are many different kinds of Join.

SQL FOR BEGINNERS SELECTING FROM MULTIPLE TABLES


What are Joins SQL-0800-2
INNER JOIN
Will retrieve data only when there is matching values in both tables.

Table 1 Table 2

SQL FOR BEGINNERS SELECTING FROM MULTIPLE TABLES


What are Joins SQL-0800-3
LEFT JOIN
Will retrieve all data from the left table (table 1) and matching rows from the right table
(table 2).

SQL FOR BEGINNERS SELECTING FROM MULTIPLE TABLES


What are Joins SQL-0800-4
RIGHT JOIN
Will retrieve all data from the right table (table 2) and matching rows from the left table
(table 1).

SQL FOR BEGINNERS SELECTING FROM MULTIPLE TABLES


What are Joins SQL-0800-5
Exercise 1
Select the order id and customers phone number for all orders of product id 4.

Select product name and order time for filter coffees sold between January 15th 2017 and
February 14th 2017.

Select the product name and price and order time for all orders from females in January
2017.

SQL FOR BEGINNERS SELECTING FROM MULTIPLE TABLES


Exercise 1 SQL-0900-1
Database Design
Section Introduction

DATABASE DESIGN
SQL-0940-1
Database
Design
NORMALIZATION
RELATIONSHIPS
CONSTRAINTS

SQL FOR BEGINNERS DATABASE DESIGN


Section Introduction SQL-0940-2
NORMALIZATION

DATABASE DESIGN
SQL-0960-1
WHAT IS NORMALIZATION
Normalization is the process of efficiently organizing
data in a database.
•Why?

•To eliminate redundant data.

•To only store related data in a table.

SQL FOR BEGINNERS DATABASE DESIGN


Normalization SQL-0960-2
WHAT IS NORMALIZATION

Student Year Class Teacher

John Smith 9 Geography Mr. Green

Tom Buchanan 9 Geography Mr. Green

Sarah Bennet 8 Physics Mrs. Einstein

Charlie Brown 9 Geography Mr. Green

April Barnes 10 Music Mrs. Sharpe

SQL FOR BEGINNERS DATABASE DESIGN


Normalization SQL-0960-3
WHAT IS NORMALIZATION
Benefits:
•Reduce storage space.
•Reduce insert, update and deletion anomalies.
•Improve query performance.

SQL FOR BEGINNERS DATABASE DESIGN


Normalization SQL-0960-4
WHAT IS NORMALIZATION
Levels of normalization:
•1st Normal Form (1NF).
•2nd Normal Form (2NF).
•3rd Normal Form (3NF).
•Boyce and Codd Normal Form (BCNF).

SQL FOR BEGINNERS DATABASE DESIGN


Normalization SQL-0960-5
FIRST NORMAL FORM
(1NF)

DATABASE DESIGN
SQL-0980-1
WHAT IS 1NF
Tables are in 1NF if:
•No repeated rows of data.

•Columns only contain a single value.

•The table has a primary key.

SQL FOR BEGINNERS DATABASE DESIGN


WHAT IS 1NF SQL-0980-2
1NF EXAMPLE

STUDENT YEAR CLASS

Paul Dawson 11 Math

Peggy Mitchell 10 History

Paul Dawson 11 Math

Brian Cox 8 English, Chemistry

Linda Marsh 7 Math, History, Biology

SQL FOR BEGINNERS DATABASE DESIGN


WHAT IS 1NF SQL-0980-3
1NF EXAMPLE

STUDENT YEAR CLASS

Paul Dawson 11 Math

Peggy Mitchell 10 History

Brian Cox 8 English

Brian Cox 8 Chemistry

Linda Marsh 7 Math

Linda Marsh 7 History

Linda Marsh 7 Biology

SQL FOR BEGINNERS DATABASE DESIGN


WHAT IS 1NF SQL-0980-4
SECOND NORMAL FORM
(2NF)

DATABASE DESIGN
SQL-1000-1
WHAT IS 2NF
Tables are in 2NF if:
•They conform to 1NF.

•Every column that is not a primary key of the table is


dependent on the whole of the primary key.

SQL FOR BEGINNERS DATABASE DESIGN


WHAT IS 2NF SQL-1000-2
2NF EXAMPLE

STUDENT SUBJECT GRADE AGE

Natasha Williams Maths A 15

Natasha Williams English B 15

Daniel James Maths C 16

Simon Brown Chemistry A 14

Emma Thomas Geography B 14

SQL FOR BEGINNERS DATABASE DESIGN


WHAT IS 2NF SQL-1000-3
2NF EXAMPLE

STUDENT SUBJECT GRADE

Natasha Williams Maths A

Natasha Williams English B

Daniel James Maths C

Simon Brown Chemistry A

Emma Thomas Geography B

SQL FOR BEGINNERS DATABASE DESIGN


WHAT IS 2NF SQL-1000-4
2NF EXAMPLE

STUDENT AGE

Natasha Williams 15

Daniel James 16

Simon Brown 14

Emma Thomas 14

SQL FOR BEGINNERS DATABASE DESIGN


WHAT IS 2NF SQL-1000-5
THIRD NORMAL FORM
(3NF)

DATABASE DESIGN
SQL-1020-1
WHAT IS 3NF
Tables are in 3NF if:
•They conform to 2NF.

•Every column that is not a primary key is only dependent on


the whole of the primary key.

SQL FOR BEGINNERS DATABASE DESIGN


WHAT IS 3NF SQL-1020-2
3NF EXAMPLE

STAR PUPIL DATE OF


SUBJECT YEAR STAR PUPIL
BIRTH

Math 2015 Matthew Taylor 1999-03-21

Physics 2015 William Edwards 1999-09-15

Chemistry 2015 Georgina Simon 1998-11-04

Math 2016 Benjamin Long 2000-05-02

Physics 2016 William Edwards 1999-09-15

SQL FOR BEGINNERS DATABASE DESIGN


WHAT IS 3NF SQL-1020-3
3NF EXAMPLE

SUBJECT YEAR STAR PUPIL

Math 2015 Matthew Taylor

Physics 2015 William Edwards

Chemistry 2015 Georgina Simon

Math 2016 Benjamin Long

Physics 2016 William Edwards

SQL FOR BEGINNERS DATABASE DESIGN


WHAT IS 3NF SQL-1020-4
3NF EXAMPLE

STAR PUPIL STAR PUPIL DATE OF BIRTH

Matthew Taylor 1999-03-21

William Edwards 1999-09-15

Georgina Simon 1998-11-04

Benjamin Long 2000-05-02

SQL FOR BEGINNERS DATABASE DESIGN


WHAT IS 3NF SQL-1020-5
RELATIONSHIPS

DATABASE DESIGN
SQL-1040-1
WHAT ARE
RELATIONSHIPS
Tables are related through primary and foreign keys.
One to One Relationship.
One to Many Relationship.
Many to Many Relationship.

SQL FOR BEGINNERS DATABASE DESIGN


RELATIONSHIPS SQL-1040-2
ONE TO ONE
RELATIONSHIP

DATABASE DESIGN
SQL-1060-1
WHAT IS ONE TO ONE
Where a key to one table appears no more than once
as the key in another table and vice versa.
1 0..1
PRODUCTS PRODUCT_DETAILS

SQL FOR BEGINNERS DATABASE DESIGN


ONE TO ONE SQL-1060-2
ONE TO MANY
RELATIONSHIP

DATABASE DESIGN
SQL-1080-1
WHAT IS ONE TO MANY
Where a primary key of one table can be in multiple
rows of a foreign key column of another table.
1 0..m
CUSTOMERS ORDERS

SQL FOR BEGINNERS DATABASE DESIGN


ONE TO MANY SQL-1080-2
MANY TO MANY
RELATIONSHIP

DATABASE DESIGN
SQL-1100-1
WHAT IS MANY TO MANY
Where two tables can have many instances of each
other.
PRODUCTS ORDERS

SQL FOR BEGINNERS DATABASE DESIGN


MANY TO MANY SQL-1100-2
WHAT IS MANY TO MANY
PRODUCTS ORDERS

1 1..m 1..m 1
ORDER_DETAILS

SQL FOR BEGINNERS DATABASE DESIGN


MANY TO MANY SQL-1100-3
CONSTRAINTS

DATABASE DESIGN
SQL-1120-1
CONSTRAINTS
NOT NULL - A column can’t contain any null values.

UNIQUE - A column can’t contain any duplicate values of data.

PRIMARY KEY - A column that uniquely identifies each row of


data.

FOREIGN KEY - A column which is related to a primary key in


another table.

SQL FOR BEGINNERS DATABASE DESIGN


CONSTRAINTS SQL-1120-2
CONSTRAINTS
CHECK - Controls the values that can be inserted into a column.

Example: CHECK(ratings BETWEEN 0 AND 100)

DEFAULT - If no values is inserted into a column, you can set a


default value.

Example: email DEFAULT ‘No Data’

SQL FOR BEGINNERS DATABASE DESIGN


CONSTRAINTS SQL-1120-3
CINEMA BOOKING SYSTEM DB
SECTION INTRODUCTION

CREATING A CINEMA BOOKING SYSTEM DATABASE


SQL-1140-1
IN THIS SECTION
Create a database for a cinema online booking system.

Consisting of 7 tables.

Explanation of the database schema.

Inserting data into our completed database.

SQL FOR BEGINNERS CREATING A CINEMA BOOKING SYSTEM DATABASE


Section Introduction SQL-1140-2
CINEMA BOOKING SYSTEM
DATABASE

CREATING A CINEMA BOOKING SYSTEM DATABASE


SQL-1160-1
DB SCHEMA

SQL FOR BEGINNERS CREATING A CINEMA BOOKING SYSTEM DATABASE


DB Schema SQL-1160-2
FILMS TABLE

CREATING A CINEMA BOOKING SYSTEM DATABASE


SQL-1180-1
Films Table

SQL FOR BEGINNERS CREATING A CINEMA BOOKING SYSTEM DATABASE


Films Table SQL-1180-2
CUSTOMERS TABLE

CREATING A CINEMA BOOKING SYSTEM DATABASE


SQL-1200-1
CUSTOMERS TABLE

SQL FOR BEGINNERS CREATING A CINEMA BOOKING SYSTEM DATABASE


CUSTOMERS TABLE SQL-1200-2
ROOMS TABLE

CREATING A CINEMA BOOKING SYSTEM DATABASE


SQL-1220-1
ROOMS TABLE

SQL FOR BEGINNERS CREATING A CINEMA BOOKING SYSTEM DATABASE


ROOMS TABLE SQL-1220-2
SCREENINGS TABLE

CREATING A CINEMA BOOKING SYSTEM DATABASE


SQL-1240-1
SCREENINGS TABLE

SQL FOR BEGINNERS CREATING A CINEMA BOOKING SYSTEM DATABASE


SCREENINGS TABLE SQL-1240-2
SEATS TABLE

CREATING A CINEMA BOOKING SYSTEM DATABASE


SQL-1260-1
SEATS TABLE

SQL FOR BEGINNERS CREATING A CINEMA BOOKING SYSTEM DATABASE


SEATS TABLE SQL-1260-2
BOOKINGS TABLE

CREATING A CINEMA BOOKING SYSTEM DATABASE


SQL-1280-1
BOOKINGS TABLE

SQL FOR BEGINNERS CREATING A CINEMA BOOKING SYSTEM DATABASE


BOOKINGS TABLE SQL-1280-2
RESERVED_SEAT TABLE

CREATING A CINEMA BOOKING SYSTEM DATABASE


SQL-1300-1
RESERVED_SEAT TABLE

SQL FOR BEGINNERS CREATING A CINEMA BOOKING SYSTEM DATABASE


RESERVED_SEAT TABLE SQL-1300-2
AGGREGATE FUNCTIONS
SECTION INTRODUCTION

AGGREGATE FUNCTIONS
SQL-1340-1
WHAT YOU WILL LEARN
What aggregate functions are.

How to use COUNT, MIN, MAX, SUM and AVG functions.

How to group data using GROUP BY clauses.

What the HAVING clause is and how to use it.

Exercise and solutions to practice.

SQL FOR BEGINNERS AGGREGATE FUNCTIONS


Section Introduction SQL-1340-2
WHAT ARE AGGREGATE
FUNCTIONS

AGGREGATE FUNCTIONS
SQL-1360-1
WHAT ARE AGGREGATE FUNCTIONS
Perform a calculations on data within a column and
returns one result row.

Can use GROUP BY clauses to group the results by


one (or more) columns.

Can use a HAVING clause in a similar way to a


WHERE clause in a SELECT statement to filter the
results set.

SQL FOR BEGINNERS AGGREGATE FUNCTIONS


WHAT ARE AGGREGATE FUNCTIONS SQL-1360-2
WHAT ARE AGGREGATE FUNCTIONS
COUNT

SUM

MIN

MAX

AVG

SQL FOR BEGINNERS AGGREGATE FUNCTIONS


WHAT ARE AGGREGATE FUNCTIONS SQL-1360-3
Exercise 1
1. How many bookings did customer id 10 make in October 2017.

2. Count the number of screenings for Blade Runner 2049 in


October 2017.

3. Count the number of unique customers who made a booking for


October 2017.

SQL FOR BEGINNERS AGGREGATE FUNCTIONS


Exercise 1 SQL-1460-1
Exercise 2
1. Select the customer id and count the number of reserved seats
grouped by customer for October 2017.

2. Select the film name and count the number of screenings for each
film that is over 2 hours long.

SQL FOR BEGINNERS AGGREGATE FUNCTIONS


Exercise 2 SQL-1540-1
SUBQUERIES
SECTION INTRODUCTION

SUBQUERIES
SQL-1580-1
WHAT YOU WILL LEARN
WHAT SUBQUERIES ARE AND THE DIFFERENT TYPES.

NON-CORRELATED SUBQUERIES

CORRELATED SUBQUERIES

EXERCISES AND SOLUTIONS

SQL FOR BEGINNERS SUBQUERIES


Section Introduction SQL-1580-2
WHAT ARE SUBQUERIES

SUBQUERIES
SQL-1600-1
WHAT ARE SUBQUERIES
Subqueries are queries nested within other queries.

SELECT id, start_time FROM screenings


WHERE film_id IN
(SELECT id FROM films
WHERE length_min > 120)
;

SQL FOR BEGINNERS SUBQUERIES


WHAT ARE SUBQUERIES SQL-1600-2
WHAT ARE SUBQUERIES
Can be used in a SELECT, INSERT, UPDATE or DELETE
query.

The nested query can be in the WHERE clause or the FROM.

Two types of subquery:

Non-correlated.

Correlated.

SQL FOR BEGINNERS SUBQUERIES


WHAT ARE SUBQUERIES SQL-1600-3
NON-CORRELATED SUBQUERY
The inner query can run independently of the outer query.

Inner query runs first and produces a result set, which is then
used by the outer query.
SQL FOR BEGINNERS SUBQUERIES
WHAT ARE SUBQUERIES SQL-1600-4
CORRELATED SUBQUERY
The inner query can’t run independently of the outer query.

SELECT SCREENING_ID, CUSTOMER_ID,


(SELECT COUNT(SEAT_ID)
FROM RESERVED_SEAT WHERE BOOKING_ID =
B.ID)
FROM BOOKINGS B;

The inner query runs for every row in the outer query.

SQL FOR BEGINNERS SUBQUERIES


WHAT ARE SUBQUERIES SQL-1600-5
Exercise 1
1. Select the film name and length for all films with a length
greater than the average film length.

2. Select the maximum number and the minimum number of


screenings for a particular film.

3. Select each film name and the number of screenings for that
film.

SQL FOR BEGINNERS SUBQUERIES


Exercise 1 SQL-1680-1
IN THIS SECTION
Learn what MySQL functions are.

Learn about the most important string functions.

Learn about the most important date functions.

SQL FOR BEGINNERS MYSQL FUNCTIONS - STRING FUNCTIONS AND DATE FUNCTIONS
Section Introduction SQL-1720-2
STRING FUNCTIONS
Concatenate

Substring

Upper

Lower

SQL FOR BEGINNERS MYSQL FUNCTIONS - STRING FUNCTIONS AND DATE FUNCTIONS
Section Introduction SQL-1720-3
DATE FUNCTIONS
Date

Month

Year

SQL FOR BEGINNERS MYSQL FUNCTIONS - STRING FUNCTIONS AND DATE FUNCTIONS
Section Introduction SQL-1720-4
IN THIS SECTION
Multiple exercises with video solutions.

Ask any question in the Q&A section.

SQL FOR BEGINNERS MYSQL FUNCTIONS - STRING FUNCTIONS AND DATE FUNCTIONS
Section Introduction SQL-1720-5
WHAT ARE MySQL FUNCTIONS?

SQL FOR BEGINNERS MYSQL FUNCTIONS - STRING FUNCTIONS AND DATE FUNCTIONS
What Are MySQL Functions? SQL-1730-1
WHAT ARE MySQL FUNCTIONS?
Functions are stored programs which can be passed parameters and return a
value.

We have already seen some MySQL functions - aggregate functions.

For example: MAX(column);

SQL FOR BEGINNERS MYSQL FUNCTIONS - STRING FUNCTIONS AND DATE FUNCTIONS
What Are MySQL Functions? SQL-1730-2
STRING FUNCTIONS
Functions that take string parameters.

SQL FOR BEGINNERS MYSQL FUNCTIONS - STRING FUNCTIONS AND DATE FUNCTIONS
What Are MySQL Functions? SQL-1730-3
DATE FUNCTIONS
Functions that take date, time or datetime parameters.

SQL FOR BEGINNERS MYSQL FUNCTIONS - STRING FUNCTIONS AND DATE FUNCTIONS
What Are MySQL Functions? SQL-1730-4
Exercise 1
Concatenate the film names and length from the films table.

Extract the customers email from the 5th character onwards.

Select the customers first name in lower case and their last name in upper case for
each customer with a last name of ‘Smith’.

Select the last 3 letters of each film name from the films table.

Concatenate the first three letters in the first_name and last_name columns together
from the customers table.

SQL FOR BEGINNERS MYSQL FUNCTIONS - STRING FUNCTIONS AND DATE FUNCTIONS
Exercise 1 SQL-1800-1
Exercise 2
Select the film id and start time from the screenings table for the date of 20th of
October 2017.

Select all the data from the screenings table for the start time between the 6th and
13th of October 2017.

Select all the data from the screenings table for October 2017.

SQL FOR BEGINNERS MYSQL FUNCTIONS - STRING FUNCTIONS AND DATE FUNCTIONS
Exercise 2 SQL-1900-1
SQL for Beginners Slides

Challenge Slides.

SQL FOR BEGINNERS


SQL for Beginners Slides
CHALLENGES!

CHALLENGES
SQL-1940-1
CHALLENGE ONE
Which films are over 2 hours long?

SQL FOR BEGINNERS CHALLENGES


Challenge One SQL-1960-1
CHALLENGE TWO
Which film had the most screenings in October 2017

SQL FOR BEGINNERS CHALLENGES


Challenge Two SQL-2000-1
CHALLENGE THREE
How many bookings did the film ‘Jigsaw’ have in October 2017

SQL FOR BEGINNERS CHALLENGES


Challenge Three SQL-2040-1
CHALLENGE FOUR
Which 5 customers made the most bookings in October 2017

SQL FOR BEGINNERS CHALLENGES


Challenge Four SQL-2080-1
CHALLENGE FIVE
Which film was shown in the Chaplin room most often in October 2017

SQL FOR BEGINNERS CHALLENGES


Challenge Five SQL-2120-1
CHALLENGE SIX
How many of the customers made a booking in October 2017

SQL FOR BEGINNERS CHALLENGES


Challenge Six 1SQL-2160-1

You might also like