T5 SQL1
T5 SQL1
2
Relational Algebra
Operations
Set operations
• Union R := A ∪ B
• Intersection R := A ∩ B
• Cartesian product R := A x B
• Difference R := A – B
Relational operations
• Selection (WHERE clause in SELECT) R1 = R2(c)
• Projection (SELECT) R1 = R2[X1, ..., Xn]
• NATURAL JOIN and other operations R1 = R1 * R2
• Implemented directly with SELECT
4
SQL
SQL (I)
• SQL = Structured Query Language.
• Created by IBM in the 70s
• Evolution of SEQUEL (Structured English QUEryLanguage)
• Interface for SYSTEM R.
• Standard Language for the relational model:
• Several versions: SQL/86, SQL2 (92), SQL3 (99, 2003, 2006)
• Differences (functionalities) exists between implementations (always check
manual).
6
SQL (II)
Characteristics:
• Declarative
• We indicate what we want and not how we want
• Data independence is a key factor
Uses of SQL:
• Interactive: the way to use it in this course
• From other languages though APIs or calls (C, C++, Java, PHP, Perl, etc.).
7
Tables / Relations
Tables
• Most important structure in the Rel. Model = relation
• In SQL are represented as tables
9
Table creation (I)
• A table with two columns
• A a column of integer type of 2 positions and primary key of the table
• B a column of type string of 30 positions (max)
10
Table creation (II)
• Table Taula2 with two columns
• C an integer of 2 positions
• D another integer of 2 positions which is a foregin key referring to Taula1
attribute A
CREATE TABLE Taula2 ( CREATE TABLE Taula1 (
C INT(3) PRIMARY KEY, A INT(2) PRIMARY KEY,
D INT(2), B VARCHAR(30));
FOREIGN KEY (D)
REFERENCES Taula1(A));
11
INSERT
INSERT
• In order to add/insert data in a table we use the INSERT INTO
command:
INSERT INTO tbl (atribut_1, atribut_2,..., atribut_n)
VALUES (val_1, val_2, ..., val_n);
• We specify as many values as attributes in the command
• Values should match the domains (types) of the corresponding attributes
• Non specified attributes will have NULL values
• In case a whole tuple is inserted no need to specify the attribute names
13
SELECT
SELECT (I)
SELECT is a very versatile command that projects specified columns… among other things
• The result of SELECT is a table, which by default is dumped to the console
• The attributes to list are (in general) column names separated by commas (,)
• Very basic syntax:
15
SELECT (II)
Suppose:
Employee(code_emp,dni,nom,cog1,cog2,codi_dept)
16
SELECT (III)
We want codes of employees:
SELECT Employee.code_emp
FROM Employee;
Code and last name:
SELECT Employee.code_emp, Employee.cog1
FROM Employee;
17
SELECT DISTINCT
SELECT may return duplicate tuples, for example:
SELECT nom
FROM Employee;
18
Aliases
Alias
It refers to a name for a table or column which could be used in the SELECT SQL
statement
• Syntax
• <col_name> AS <col_alias>
• <table_name> AS <table_alias>
• The alias will be used in the resulting table and it is a way to implement the
rename operation.
• These two queries are equivalent:
SELECT code_emp AS code
FROM Employee;
20
WHERE
WHERE (I)
WHERE allows to apply restrictions on tuples to be selected (e.g. restriction)
Syntax:
SELECT col1, col2, ...
FROM table
WHERE condition;
where condition:
• Is a logic expression which evaluates to a boolean
• Could be:
• Simple, a single terms such as codi_emp > 10
• Complex, combining several terms with logic:
code_emp >10 AND code_dep = “D5”
22
WHERE (II)
Example:
List all employees (code and full name) who have employee code
grater than 1000:
23
SQL and Relational Algebra
24
Logical conditions
Comparison: =, >, <, >=, <=, <>
One can compare with a constant or an attribute or an expression
• salary > 1000
• 1000 = salary
• nom = cognoms
• 1000 > 30000
• Expressions are evaluated for each tuple
• Attributes in expressions should refer to the selected tables in SELECT.
• Constant are those of the given types in SQL (strings, integers, Boolean,
dates, etc.)
25
More complex logical conditions: AND, OR,
NOT
Operators: AND, OR, NOT
If exp1, exp2 and exp are logical expressions then:
• exp1 AND exp2
• exp1 OR exp2
• NOT exp
Are also logical expressions
Examples:
• (salary > 1000 AND salary < 2000)
• (salary = 1000 OR code_dept = “D5”)
• NOT salary = 1000
26
Logic conditions: BETWEEN
Operator BETWEEN, syntax:
WHERE <attribut> [NOT] BETWEEN <limit_1> AND <limit_2>
Examples:
SELECT code_emp
FROM Employee
WHERE salary BETWEEN 21000 AND 40000;
SELECT code_emp
FROM Employee
WHERE salary NOT BETWEEN 21000 AND 40000;
27
Logic conditions: IN
Operator IN, syntax:
WHERE <attribut> [NOT] IN (val1, ..., valN)
Examples:
SELECT code_emp
FROM Employee
WHERE code_dept IN ( ‘D5’, ‘D6’ );
SELECT code_emp
FROM Employee
WHERE code_dept NOT IN (‘D5’, ‘D6’ );
28
Logic conditions: LIKE
Operator LIKE, syntax:
WHERE attribut [NOT] LIKE <patterns>
Patró Significat
‘ABC%’ Starts with ‘ABC’
‘%ABC’ Ends with ‘ABC’
‘%ABC%’ Contains ‘ABC’
‘_A%’ Contains ‘A’ in second position
‘_A_’ Has 3 characters with an A as second
‘A_%’ Starts with ‘A’ has at least 2 chars
‘A%O’ Starts with A and ends in O
30
Logical conditions: example
Any employee whose surname starts with “A”:
SELECT code_emp, nom
FROM Employee
WHERE cog1 LIKE “A%”;
Employees whose surname has “a” as second letter:
SELECT code_emp, nom
FROM Employee
WHERE cog1 LIKE “_a%”;
31
Logical conditions: IS NULL
Operator IS NULL, syntax:
WHERE attribut IS [NOT] NULL
• We use it to searcj for NULL
• We never use “=“ or “<>” to compare with NULL
Examples:
SELECT code_emp, nom SELECT code_emp, nom
FROM Employee FROM Employee
WHERE cog2 IS NULL; WHERE cog2 IS NOT NULL;
32
ORDER BY
Sorting the results: ORDER BY
Operator ORDER BY used to provide a particular order to the tuples
displayed by the SELECT.
Syntax:
SELECT col1, col2, col3...
FROM table
ORDER BY col1, col2, ... ASC|DESC;
By default ASC
Examples:
SELECT * FROM Employee SELECT * FROM Employee
ORDER BY code_emp DESC; ORDER BY code_emp DESC, nom;
34
Aggregation operators in SQL
Aggregation operators
• Allow summarization of the data:
36
Aggregation: Examples
How many tuples in employees table:
SELECT COUNT(*)
FROM Employee;
How many non NULL values in the names of employees.
SELECT COUNT(nom)
FROM Employee;
• If there are no NULL the result will be the same as before
Average salary of employees from department D5:
SELECT AVG(salary)
FROM Employee
WHERE Dept = ‘D5’;
37