Week 8 Structured Query Language (SQL)

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 38

Week 8

Structured Query Language (SQL)


Writing SQL Commands

• SQL statement consists of reserved words and user-


defined words.

– Reserved words are a fixed part of SQL and must be


spelt exactly as required and cannot be split across
lines.
– User-defined words are made up by user and
represent names of various database objects such as
relations, columns, views.

2
Writing SQL Commands

• Use extended form of BNF notation:

- Upper-case letters represent reserved words.


- Lower-case letters represent user-defined words.
- | indicates a choice among alternatives.
- Curly braces indicate a required element.
- Square brackets indicate an optional element.
- … indicates optional repetition (0 or more).

3
CREATE DATABASE
• The CREATE DATABASE statement is used to
create a database.
• Syntax:
CREATE DATABASE dbname;

• Example:
CREATE DATABASE studentInformationSystem;
USE STATEMENT
• The USE statement is used to choose a
database.
• Syntax:
USE dbname;

• Example:
USE studentInformationSystem;
CREATE TABLE
• The CREATE TABLE statement is used to create a table in a
database.
• Tables are organized into rows and columns
• Table must have a name
• Syntax:
CREATE TABLE table_name
(
column_name1 data_type(size),
column_name2 data_type(size),
column_name3 data_type(size),
....
);
An ERD example

matrix name course_code

STUDENT REGISTER COURSE

age email course_name credit

address
Tables for ERD
STUDENT
matrix name age address email

COURSE
course_code course_name credit matrix
CREATE TABLE
• The CREATE TABLE statement is used to create a table in a
database.
• Tables are organized into rows and columns
• Table must have a name
• Syntax:
CREATE TABLE table_name
(
column_name1 data_type(size),
column_name2 data_type(size),
column_name3 data_type(size),
....
);
CREATE TABLE EXAMPLE
STUDENT
matrix name age address email

The SQL codes:


CREATE TABLE student
(
matrix int,
name varchar(255),
age int,
address varchar(255),
email varchar(25),
PRIMARY KEY (matrix)
);
CREATE TABLE EXAMPLE (cont.)
COURSE
course_code course_name credit matrix

The SQL codes:


CREATE TABLE course
(
course_code varchar(10),
course_name varchar(25),
credit int,
PRIMARY KEY (course_code),
FOREIGN KEY (matrix) REFERENCES student(matrix)
);
STUDENT
matrix name age address Email
113100 Nurfatin binti 19 No. 8, Jalan 2/4, Taman fatin@usim.edu.my
Mohd Seri Serdang, 43300
Seri Kembangan,
Selangor
113101 Siti binti Samuri 19 No. 34, Jalan Lili 2, siti@usim.edu.my
Taman Anggerik, 71800
Nilai,
Negeri Sembilan
SQL INSERT INTO Statement

• The INSERT INTO statement is used to insert


new records in a table.
• Syntax:

INSERT INTO table_name


VALUES (value1,value2,value3,...);
INSERT INTO EXAMPLE
The SQL codes:
INSERT INTO student (matrix, name, age, address, email)
VALUES ('113100', 'Nurfatin binti Mohd', '19','No. 8, Jalan
2/4, Taman Seri Serdang, 43300 Seri Kembangan, Selangor ',
'fatin@usim.edu.my');

INSERT INTO student (matrix, name, age, address, email)


VALUES ('113101', 'Siti binti Samuri', '19','No. 34, Jalan Lili 2,
Taman Anggerik, 71800 Nilai, Negeri Sembilan',
'siti@usim.edu.my');
SQL SELECT STATEMENT
• The SELECT statement is used to display
records from a table
• The following SQL code will display all records
from table student:
SELECT *FROM student;
SELECT Statement

FROM Specifies table(s) to be used.


WHEREFilters rows.
GROUP BY Forms groups of rows with same
column value.
HAVING Filters groups subject to some
condition.
SELECT Specifies which columns are to
appear in output.
ORDER BY Specifies the order of the output.

16
Example: All Columns, All Rows

Purpose: To list full details of all staff.


SELECT staffNo, fName, lName, address,
position, sex, DOB, salary, branchNo
FROM Staff;

• Can use * as an abbreviation for ‘all columns’:


SELECT *
FROM Staff;

17
Example All Columns, All Rows

18
Example: Specific Columns, All Rows

Produce a list of salaries for all staff, showing only


staff number, first and last names, and salary.

SELECT staffNo, fName, lName, salary


FROM Staff;

19
Example: Specific Columns, All Rows

20
Example: Use of DISTINCT

List the property numbers of all properties that have


been viewed.

SELECT propertyNo
FROM Viewing;

21
Example: Use of DISTINCT
• Use DISTINCT to eliminate duplicates:

SELECT DISTINCT propertyNo


FROM Viewing;

22
Example: Calculated Fields

Produce list of monthly salaries for all staff,


showing staff number, first/last name, and
salary.

SELECT staffNo, fName, lName, salary/12


FROM Staff;

23
Example: Calculated Fields

• To name column, use AS clause:


SELECT staffNo, fName, lName, salary/12
AS monthlySalary
FROM Staff;

24
Example: Comparison Search Condition

List all staff with a salary greater than 10,000.


SELECT staffNo, fName, lName, position, salary
FROM Staff
WHERE salary > 10000;

25
Example: Range Search Condition

List all staff with a salary between 20,000 and


30,000.

SELECT staffNo, fName, lName, position, salary


FROM Staff
WHERE salary BETWEEN 20000 AND 30000;

• BETWEEN test includes the endpoints of range.

26
Example: Range Search Condition

27
Example: Range Search Condition

• Also a negated version NOT BETWEEN.


• BETWEEN does not add much to SQL’s
expressive power. Could also write:

SELECT staffNo, fName, lName, position, salary


FROM Staff
WHERE salary>=20000 AND salary <= 30000;

• Useful, though, for a range of values.

28
Example: Pattern Matching

Find all owners with the string ‘Glasgow’ in their address.

SELECT ownerNo, fName, lName, address, telNo


FROM PrivateOwner
WHERE address LIKE ‘%Glasgow%’;

29
Example: Single Column Ordering

List salaries for all staff, arranged in descending


order of salary.

SELECT staffNo, fName, lName, salary


FROM Staff
ORDER BY salary DESC;

30
Example: Single Column Ordering

31
SELECT Statement - Aggregates

ISO standard defines five aggregate functions:

• COUNT returns number of values in specified


column.
• SUM returns sum of values in specified column.
• AVG returns average of values in specified
column.
• MIN returns smallest value in specified column.
• MAX returns largest value in specified column.
32
Example: Use of MIN, MAX, AVG

Find minimum, maximum, and average staff


salary.
SELECT MIN(salary) AS myMin,
MAX(salary) AS myMax,
AVG(salary) AS myAvg
FROM Staff;

33
Example: Use of Count

Count the number of staff


SELECT COUNT(staffNo) AS numberOfStaff
FROM Staff;

34
Example: Use of Count

Count the number of staff


SELECT COUNT(staffNo) AS numberOfStaff
FROM Staff;

numberOfStaf
numberOfStaff

35
Example: Subquery of equality
• The following SQL code will display certain
records from table student based on the
condition given:
SELECT *FROM student
WHERE matrix=113100;

OUTPUT:
matrix name age address Email
113100 Nurfatin binti 19 No. 8, Jalan 2/4, Taman fatin@usim.edu.my
Mohd Seri Serdang, 43300
Seri Kembangan,
Selangor
SQL DELETE STATEMENT
• The DELETE statement is used to delete rows
in a table.
• Syntax:
DELETE FROM table_name
WHERE some_column=some_value;

• Example:
DELETE FROM student
WHERE matrix=‘113100’;
SQL DROP TABLE
• The DROP TABLE statement is used to delete
a table in a database.
• Syntax:
DROP TABLE table_name;

• Example:
DROP TABLE student;

You might also like