0% found this document useful (0 votes)
20 views37 pages

T5 SQL1

This document provides an introduction to SQL and relational algebra. It outlines the objectives of learning SQL and its sublanguages for data manipulation (DML) and data definition (DDL). It describes basic syntax for creating tables, inserting tuples, and retrieving data using relational algebra operations. It also covers SQL concepts like SELECT statements, aliases, WHERE clauses, logical conditions and operators for selection.

Uploaded by

Leo Barcino
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views37 pages

T5 SQL1

This document provides an introduction to SQL and relational algebra. It outlines the objectives of learning SQL and its sublanguages for data manipulation (DML) and data definition (DDL). It describes basic syntax for creating tables, inserting tuples, and retrieving data using relational algebra operations. It also covers SQL concepts like SELECT statements, aliases, WHERE clauses, logical conditions and operators for selection.

Uploaded by

Leo Barcino
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 37

SQL introduction

Relational model Implementation


Objectives
1. The SQL language.
2. SQL sub‐languages:
a. DML (Data Manipulation Language)
b. DDL (Data Definition Language) create tables, etc.
3. Basic syntax for table (relation) creation
4. Basic syntax for tuple insertion
5. Retrieving data from tables and relation with Relational Algebra

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

Data types Restrictions


• char(n) (opcional):
• Basic syntax for creating a table: • varchar(n) • PRIMARY KEY
CREATE TABLE <TBL_NAME> ( • int(n) • FOREIGN KEY
• float(n) • NOT NULL
col_1 type [restriction], • date • UNIQUE
col_2 type, • time • …
... • …
);

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)

CREATE TABLE Taula1 (


A INT(2) PRIMARY KEY,
B VARCHAR(30)
);

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:

SELECT [DISTINCT] col1, col2, ...


FROM table1;
FROM indicates which is the table (tables) we want to select the tuples from
• It is possible to specify multiple tables, which has a very specific meaning
• In occasions we need to specify to which table a column belongs to (in case of ambiguity)

15
SELECT (II)
Suppose:
Employee(code_emp,dni,nom,cog1,cog2,codi_dept)

We want code and full name of employees:


SELECT code_emp, nom, cog1, cog2
FROM Employee;
We want to see all information about employees:
SELECT *
FROM Employee;
NOTE: End of SQL statement is a “;”

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;

Since there may be many employees with the same name.


The keyword DISTINCT can be used to return different tuples
SELECT DISTINCT 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;

SELECT E.code_emp AS code


FROM Employee E;

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:

SELECT code_emp, nom, cognoms


FROM Employee
WHERE code_emp > 1000;

23
SQL and Relational Algebra

SQL Relational Algebra


SELECT A, B Projection:
FROM R; R[A, B]
SELECT * Selection/Restriction
FROM R R(A > 10 AND B=“Hello”)
WHERE A > 10 AND B=“Hello”;
SELECT A Selection + Projection
FROM R R(A > 10 AND B=“Hello”)[A]
WHERE A > 10 AND B=“Hello”;

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>

• attribut is of type string


• Pattern is a string
• In general it is used for “pattern matching”
• For exact match the “=“ should be used.
Exemple:
SELECT code_emp, nom
FROM Employee
WHERE cog1 LIKE “Mart_%”;
29
Logic conditions: LIKE wildcards
Wildcards in expressions LIKE are used for matching strings
• ‘%’ means zero or more characters
• ‘_’ means one character

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:

• COUNT(col): counts the number of selected tuples


• SUM(col): adds the column of the selected tuples
• MIN(col): returns the min value of the selected tuples
• MAX(col): ): returns the max value of the selected tuples
• AVG(col): returns the average value of the selected tuples
• NULL values are not considered in the aggregation operators.
• They are generally used in the list of columns to be listed.
• Syntax:
SELECT AGGFUNC(col)
FROM table AGGFUNC is one of the funcions/operators
[WHERE condition];
WHERE is optional

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

You might also like