8.3 Structured Query Language EDITED
8.3 Structured Query Language EDITED
DDL – A language used to create, modify and remove the data structures that form a database.
DML – A language used to add, modify, delete and retrieve the data stored in a relational database.
DDL Commands
in
CREATE DATABASE Creates a database
CREATE TABLE Creates a table definition
ALTER TABLE Changes the definition of a table
PRIMARY KEY Adds a primary key to a table
FOREIGN KEY … REFERENCES … Adds a foreign key to a table
st
Data Types for Attributes Description
CHARACTER
VARCHAR(n)
BOOLEAN
u Fixed length text
Variable length text
True or False; SQL uses the integers 1 and 0
g
INTEGER Whole number
REAL Number with decimal places
u
DATE A date usually formatted as YYYY-MM-DD
TIME A time usually formatted as HH:MM:SS
A
CREATE
SQL:
Page 1 of 11
A Level Computer Science P1 - Databases
ALTER
Altering table Movies and setting the primary key.
SQL:
Adding the primary key after the table has been created. This can also be done during the table creation.
in
SQL:
st
SQL:
u
g
ALTER TABLE Movies ADD Duration INTEGER;
u
DML Commands
r
Page 2 of 11
A Level Computer Science P1 - Databases
SELECT
To display all items from table Movies:
SQL:
in
To display MovieID and MovieTitle released after year 2017 from table Movies:
st
SQL:
SQL:
SELECT MovieTitle
FROM Movies
WHERE MovieType = 'SciFi' ;
M
Page 3 of 11
A Level Computer Science P1 - Databases
SQL:
SELECT MovieTitle
FROM Movies ;
in
To display the MovieTitle and Price of all movies with a Duration of more than 90 minutes and of
st
MovieType Comedy.
SQL:
SELECT MovieTitle, Price
FROM Movies
WHERE Duration > 90 AND MovieType = 'Comedy';
u
g
u
A
To display MovieTitle and Price of all movies with duration of less than 120 minutes or a price equal to $10.
SQL:
r.
Page 4 of 11
A Level Computer Science P1 - Databases
ORDER BY
To arrange (sort)records in specified order
SQL:
SELECT MovieID, MovieTitle
FROM Movies
ORDER BY MovieTitle ;
in
st
SQL:
SELECT MovieID, MovieTitle
FROM Movies
ORDER BY MovieTitle DESC ; u
g
u
A
r.
SQL:
SELECT MovieID, MovieTitle, YearReleased
M
FROM Movies
ORDER BY YearReleased ASC ;
Page 5 of 11
A Level Computer Science P1 - Databases
INSERT
To add a new record into the table Movie
SQL:
INSERT INTO Movies
VALUES ('SF188', 'Men in Black 4', 'SciFi', 2019, 50, 30, 90);
in
To add values for specific fields only.
SQL:
INSERT INTO Movies ( MovieId, MovieTitle, YearReleased, Duration )
st
VALUES ('SF213', 'Wonder Woman', 2017, 90);
u
g
u
DELETE
A
SQL:
DELETE FROM Movies
r.
Page 6 of 11
A Level Computer Science P1 - Databases
SQL:
UPDATE Movies
SET Duration = 130
WHERE MovieID = 'SF100';
GROUP BY
This is used with the SELECT statement to arrange identical data in groups. It is often used with aggregate
in
functions (COUNT, SUM, AVG…)
st
To list the number of movies (according to their type) for each duration:
SQL:
SELECT COUNT(MovieType), Duration
FROM Movies
u
g
GROUP BY Duration;
u
A
r.
To list the number of movies (according to their duration) for each type of movie:
M
SQL:
SELECT COUNT(Duration), MovieType
FROM Movies
GROUP BY MovieType;
Page 7 of 11
A Level Computer Science P1 - Databases
INNER JOIN
This statement retrieves data from two tables at least.
…………………………………………………………………………………………………………………………………………………………………………………………….
in
Some records in table Suppliers
st
u
g
…………………………………………………………………………………………………………………………………………………………………………………………….
SQL:
u
Page 8 of 11
A Level Computer Science P1 - Databases
SQL:
SELECT Movies.MovieType, Movies.MovieTitle, Suppliers.SupplierName,
Suppliers.SupplierAddress
FROM Movies INNER JOIN Suppliers
WHERE Movies.MovieID = Suppliers.MovieID
AND Movies.YearReleased > 2015;
NOTE:
1.
# is used to enclose a date value.
in
SELECT StaffID
FROM STAFF
WHERE CourseTitle = 'Excel Stage 1 ' AND CourseDate = #13/05/2022# ;
st
2.
The INNER JOIN keywords select all tuples from both tables where there is a match between the
columns in both tables.
u
The table name is added in front of the attribute name, using the 'dot notation'.
g
u
3.
Query Functions
The following three functions are available to use in a query. They all take an attribute name as
A
The following are examples of theitr use and so are called aggregate functions.
r.
ii. Calculate and show the average number of years of service for all the members of staff.
SELECT AVG (YearsService)
FROM STAFF ;
iii. Calculate the total number of hours training done by the staff member with ID = 89.
SELECT SUM (Duration)
FROM STAFF
WHERE StaffID = 89 ;
Page 9 of 11
4. Adding a foreign key to a table
In brief:
When creating tables,
• Each field definition must be separated (from the next) by a comma.
in
• Statements can be written on a single line but they are much easier to comprehend when split across several lines.
• When using multiple lines, you must not forget to specify the parentheses (brackets) and commas.
• Usually, the first line includes the opening parenthesis and the last line will include the matching closing
parenthesis and semicolon.
st
• Whilst SQL is not case sensitive, keywords such as SELECT and FROM are written in upper case. This is good
u
practice as it helps to make your SQL statements more readable.
• It is a requirement that a semicolon (;) is placed at the end of the final line of the query to complete the SQL
statement before executing it.
g
u
A
r.
M
Page 10 of 11
Other sample SQL for INNER JOIN
SELECT Movies.MovieType, Movies.MovieTitle, Suppliers.SupplierName,
Suppliers.SupplierAddress
FROM Movies
INNER JOIN Suppliers
ON Movies.MovieID = Suppliers.MovieID ;
Page 11 of 11