S07 Slides
S07 Slides
Seminar 7:
SQL
Learning Objectives
In this lecture, you will learn
SQL introduction
SQL syntax
1
7/22/2020
Database Fundamentals
• Database contains Database Tables, each
identified by a table name.
• Each Tables contains rows or records.
• Each record contains
columns/fields/attributes (these 3 terms refer
to the same thing).
• Every column has a name and contains data
of a specific type : string, integer, real(float)
and so on.
2
7/22/2020
SQL Overview
• The standard language for relational database
management systems (RDBMS).
• RDBMS: A database management system that
manages data as a collection of tables in which all
relationships are represented by common values
in related tables.
• The American National Standards Institute (ANSI)
prescribes a standard SQL. The ANSI SQL is also
accepted by International Organization for
Standardization (ISO).
3
7/22/2020
SQL Introduction
• Relatively easy to learn
• Several SQL dialects exist: DB2, MySQL,
MSSQL, Oracle, SQLite, etc.
• Specify syntax/semantics for data
definition and manipulation.
• Define data structures.
• Enable portability.
7
SQL Environment
• Catalog
– A set of schemas that constitute the description of a database
• Schema
– The structure that contains descriptions of objects created by user.
• Data Definition Language (DDL)
– Commands to create, alter, drop database objects like tables, indexes,
and views.
• Data Manipulation Language (DML)
– Commands to insert, update, delete, and retrieve database objects.
• Data Control Language (DCL)
– Commands to control and administer privileges.
4
7/22/2020
10
10
5
7/22/2020
11
11
1. Creating Database
• The first step in creating database is to create a
structure for database -> Schema.
• The relevant physical files that will hold the
database will then be initialized together with
administrator rights.
• For Microsoft Access, creation of database is
straightforward. Other RDBMS requires SQL
commands like the following:
– CREATE SCHEMA AUTHORIZATION
CREATOR_NAME;
12
12
6
7/22/2020
SQL Constraint
NOT NULL constraint
◦ Ensures that a column does not accept nulls
UNIQUE constraint
◦ Ensures that all values in a column are unique
DEFAULT constraint
◦ Assigns a value to an attribute when a new row is added to a table
CHECK constraint
◦ Validates data when an attribute value is entered
13
13
14
14
7
7/22/2020
2. Creating Table
Structures with
Foreign Key
CREATE TABLE EXECUTIVE (
EXECUTIVE_CODE INTEGER
NOT NULL UNIQUE,
NAME VARCHAR(50) NOT
NULL,
MANAGER_CODE INTEGER,
For every manager, there
PRIMARY KEY will be many executives
(EXECUTIVE_CODE),
FOREIGN KEY Executive
(MANAGER_CODE) Manager
REFERENCES
PK Executive_Code
PK Manager_Code manages
MANAGER(MANAGER_CODE) Name
Name
); FK Manager_Code
15
16
16
8
7/22/2020
17
17
3. Insert Data to
Table
• Syntax:
– INSERT INTO
TABLE_NAME VALUES (X,
Y, Z);
• Example:
– INSERT INTO MANAGER
VALUES (1002, "Mary" ); Executive
Manager
• Missing value: PK Executive_Code
PK Manager_Code
– If null is allowed, add in the
manages
Name
18
18
9
7/22/2020
19
19
4. Querying Data
• Syntax:
– SELECT COLUMN_LIST FROM TABLE_NAME;
• Example:
– SELECT MANAGER_CODE, NAME FROM MANAGER;
• This will list out manager_code and name from
manager table.
• Equivalent SQL using * asterisk to denote list out all
fields.
– SELECT * FROM MANAGER;
• Select unique values.
– SELECT DISTINCT NAME FROM MANAGER;
20
20
10
7/22/2020
21
21
22
11
7/22/2020
WHERE clause
• WHERE condition
– SELECT NAME, PORTFOLIO_VALUE FROM MANAGER
WHERE PORTFOLIO_VALUE >= 50000
ORDER BY PORTFOLIO_VALUE;
• Boolean Operators to customize WHERE condition
– and, or, not
• Comparison Operators
– =. <, <=, >, >=, <> (not equal)
AS for giving alias
• Arithmetic Operators name to the result.
– +, -, *, /, ^ (to the power of)
– SELECT PORTFOLIO_VALUE, PORTFOLIO_VALUE * 1.3 AS USD_EQV
FROM MANAGER;
23
23
Other Operators
• BETWEEN SELECT * FROM MANAGER
– Look for values within a range WHERE PORTFOLIO_VALUE BETWEEN 30000 AND 50000;
• IS NULL
– Check if an attribute value is null
• LIKE SELECT * FROM MANAGER
– Matching pattern with string WHERE NAME LIKE ‘Je%';
• EXISTS
– Check if the sql statement returns any result
• IN
– Check existence of value in a list of value
24
24
12
7/22/2020
GROUP BY Statement
• GROUP BY clause is used to create frequency
distribution of data, grouping data into a few groups to
summarise the data.
• It is often used in conjunction with aggregate functions.
– SELECT COUNT(*),DEPARTMENT FROM MANAGER
GROUP BY DEPARTMENT;
25
25
HAVING + GROUP BY
• Having filters results after GROUP BY.
• It sets the condition with comparison operators and
displays only results that satisfies this condition
– SELECT COUNT(*),DEPARTMENT FROM MANAGER
GROUP BY DEPARTMENT
HAVING COUNT(*) >= 2;
26
26
13
7/22/2020
Joining tables
Executive
Manager
PK Executive_Code
PK Manager_Code manages
Name
Name
FK Manager_Code
27
27
Join clause
• SQL Join clause is used to combine records from
two or more tables.
• Join will combine the respective fields set within
JOIN requirements and match the values.
• INNER JOIN combines the results similar to AND
operation look for values common to both tables.
• OUTER JOIN keeps the non-matching results
when join is done.
28
28
14
7/22/2020
Inner Join
Executive
Manager
PK Executive_Code
PK Manager_Code manages
Name
Name
FK Manager_Code
29
29
30
30
15
7/22/2020
31
31
Subqueries
• For nested selection, subquery can be used
to generate information. Runs the inner query
first, then apply outer queries on the inner
queries.
32
16
7/22/2020
IN Operator
• The IN operator allows multiple values to be specified to
match with the column name.
• SELECT NAME FROM MANAGER
WHERE MANAGER_CODE IN (1001, 1003);
33
33
34
34
17