Nazarbayev University Seds CS: CSCI 341, Fall 2024
Nazarbayev University Seds CS: CSCI 341, Fall 2024
SEDS CS
CSCI 341, Fall 2024
Instructor: Yesdaulet Izenov
Lecture
Structured Query Language (SQL)
Data Definition Language (DDL)
Constraints
Relational Data Model
Data Model properties: structure, constraints and operations
Structured data - CSV :: ~25k out of 50k datasets
Semistructured data - XML/JSON :: ~3k out of 50k datasets
Unstructured data - images, video, audio, etc.
Schema of a relation
Rows (tuple, record)
Columns (attribute, feature, field)
Structured Query Language (aka. SQL)
Declarative language (abstraction)
No Direct Access to Database Storage
Only via SQL language to interact with (R)DBMS to consequently access databases
)
Column Constraints (Data Types)
Primitive data types only (no containers)
INTEGER
DECIMAL (3, 2) – total digits, decimal digits. Ex: -9.99 to +9.99
CHAR (10) – memory allocation size (fixed size): 10 bytes
VARCHAR (10) – memory allocation size (variable size): 1,2,…10 bytes
DATE
etc.
DROP TABLE / DATABASE
DROP DATABASE db_name Table/Relation is deleted from the database
Table schema/structure is deleted from the database
DROP TABLE table_name All tuples/rows are deleted from the database
Lecture
Key Constraints
Data Manipulation Language
Column/Row Constraints (PRIMARY KEY)
Applied on a single attribute
Applied on multiple attributes (composite key)
Distinct/different attribute values across all the tuples/rows in a table
no two tuples with the same Primary Key value
No NULL values on the Primary Key
Primary Key attribute builds an index – useful for query optimization
There can be only one Primary Key per table
Column/Row Constraints (PRIMARY KEY)
CREATE TABLE table_name (
attribute_name1 DATA_TYPE PRIMARY KEY,
attribute_name2 DATA_TYPE
)
attribute_name1 DATA_TYPE,
attribute_name2 DATA_TYPE,
attribute_name3 DATA_TYPE,
attribute_name5 DATA_TYPE,
attribute_name6 DATA_TYPE,
)
Assertions
CREATE ASSERTION assertion_name
CHECK (
boolean condition
);
Data Conditions To /From Database Constraints
Data Model properties:
1. Structure
2. Constraints
3. Operations
INSERT INTO table_name (attribute_name1, attribute_name2) VALUES (value1, value2), (value3, value4);
FROM table_name2;
UPDATE table_name
SET attribute_name1 = value1, attribute_name2 = value2;
UPDATE table_name
SET attribute_name1 = value1, attribute_name2 = value2
WHERE attribute_name1 > value3;
UPDATE table_name1
SET attribute_name1 = value1, attribute_name2 = value2
WHERE attribute_name1 IN (SELECT attribute_name2 FROM table_name2);
DELETE existing tuples
Lecture
Data Query Language (DQL)
Relational Data Model (Schema)
Data Model properties: structure, constraints, and operations
Schema of a relation
Rows (tuple, record)
Columns (attribute, feature, field)
Structured Query Language (aka. SQL)
Declarative language (abstraction)
No Direct Access to Database Storage
Only via SQL language to interact with (R)DBMS to consequently access databases
SELECT *
FROM Laptop;
Projection (SELECT) on a single Relation
SELECT column(s) ← (output attributes = result table)
Question example:
FROM input_table(s);
How much do laptops cost?
SELECT price
FROM Laptop;
Alias (AS)
Query readability
SELECT Product.model, Product.maker, Product.type
Fast query writing (shorter table and column names)
FROM Product;
Renaming input tables and output columns
To eliminate ambiguity in choosing column names
SELECT model, maker, type
Naming expression and calculation FROM Product;
FROM Product;
SELECT 5 AS my_value
FROM Product;
SELECT *
FROM Product, Laptop
WHERE Product.model = Laptop.model;
SELECT *
FROM Product AS P, Laptop AS L
WHERE P.model = L.model;
Alias (AS)
SELECT *
FROM Product, Product ← AMBIGUITY
WHERE model != model; ← AMBIGUITY
SELECT *
FROM Product AS pr1, Product AS pr2
WHERE pr1.model != pr2.model;
Alias (AS)
Query readability
SELECT Product.model, Product.maker, Product.type
Fast query writing (shorter table and column names)
FROM Product;
Renaming input tables and output columns
To eliminate ambiguity in choosing column names
SELECT model, maker, type
Naming expression and calculation
FROM Product;
Referencing in other clauses
Naming subquery
SELECT model AS mo, maker AS ma, type AS t
FROM Product;
SELECT COUNT(*)
FROM Laptop;
or you can write
SELECT COUNT(hd)
FROM Laptop;
SELECT LENGTH(maker)
FROM Product;
WHERE conditions;
SELECT speed
FROM Laptop
WHERE speed >= 2.00;
Selection predicates in WHERE clause
Tree-Valued Logic: True, False, NULL (unknown)
SELECT column(s) ← (output attributes = result table)
FROM input_table(s)
CONJUNCTION: AND
WHERE conditions;
DISJUNCTION: OR
NEGATION: NOT
STRING COMPARISONS: %, _, =
Membership checking: IN
Grouping (GROUP BY) on a single Relation
Question example:
SELECT MIN(price)
What is cheapest price for laptops in each ram size category? FROM Laptop
GROUP BY ram;
Selection in Grouping (GROUP BY + HAVING)
SELECT ram
Question example:
FROM Laptop
What ram sizes have at least 3 different hard-drive sizes?
GROUP BY ram
HAVING COUNT(DISTINCT hd) >= 3;
Sorting ( ORDER BY [ASC | DESC] )
Question example:
SELECT *
Show the laptops sorted by their price from expensive to cheapest?
FROM Laptop
ORDER BY price DESC;
Sorting ( ORDER BY [ASC | DESC] )
Question example:
SELECT *
Show the laptops sorted by their ram size and price from expensive to
cheapest? FROM Laptop
ORDER BY ram, price DESC;
Select first rows (LIMIT)
Question example: SELECT *
Show first 5 laptop details?
FROM Laptop
LIMIT 5;
Question Decomposition
Question example:
What ram sizes have at least 3 different hard-drive sizes?
SELECT ram
FROM Laptop
GROUP BY ram
HAVING COUNT(DISTINCT hd) >= 3;
Nazarbayev University
SEDS CS
CSCI 341, Fall 2024
Instructor: Yesdaulet Izenov
Lecture
Data Query Language (DQL)
Joins
Data Query Language (aka. DQL)
Operations on a SINGLE table:
1. Input (FROM) – input table
2. Projection (SELECT) – cut table horizontally
The result is the output of the question or input to another SQL query
WHERE join_predicates;
SELECT *
FROM Product, Laptop;
(join not specified)
SELECT *
FROM Product JOIN Laptop;
SELECT *
FROM Product CROSS JOIN Laptop;
Cross Join Question example:
WHERE join_predicates;
WHERE join_predicates;
SELECT *
FROM Product, Laptop
WHERE Product.model = Laptop.model;
SELECT *
FROM Product INNER JOIN Laptop ON Product.model = Laptop.model;
SELECT *
FROM Product JOIN Laptop ON Product.model = Laptop.model;
Equi Inner Join
Question example:
SELECT *
FROM Product INNER JOIN Laptop ON Product.model > Laptop.model;
SELECT *
FROM Product JOIN Laptop ON Product.model > Laptop.model;
Theta Inner Join
Natural Inner Join
SELECT column(s) ← (output attributes = result table)
FROM input_table(s);
SELECT *
FROM Product NATURAL JOIN Laptop;
WHERE join_predicates;
SELECT *
FROM Product FULL OUTER JOIN Laptop
ON Product.model = Laptop.model;
SELECT *
FROM Product FULL JOIN Laptop ON
Product.model = Laptop.model;
Full Outer Join
Left Outer Join
SELECT column(s) ← (output attributes = result table)
Inner Join +
FROM input_table(s);
Unmatched Left table results
SELECT *
FROM Product LEFT OUTER JOIN Laptop
ON Product.model = Laptop.model;
SELECT *
FROM Product LEFT JOIN Laptop ON
Product.model = Laptop.model;
Left Outer Join
Right Outer Join
SELECT column(s) ← (output attributes = result table)
Inner Join +
FROM input_table(s);
Unmatched Right table results
SELECT *
FROM Product RIGHT OUTER JOIN
Laptop ON Product.model = Laptop.model;
SELECT *
FROM Product RIGHT JOIN Laptop ON
Product.model = Laptop.model;
Right Outer Join
Self Join
SELECT column(s) ← (output attributes = result table)
FROM input_table(s)
WHERE join_predicates;
SELECT *
FROM Product AS pr1, Product AS pr2
WHERE pr1.model < pr2.model;
Self Join