You are on page 1of 20

dit

UPM

Information Systems
~ Database Use~
Jose M. del Alamo

Departamento de Ingeniería de
Sistemas Telemáticos
dit
UPM
Data manipulation
SQL (Structured Query Language) is a declarative language to let
the database know what you want to achieve:
◆ Data Definition Language (DDL): To implement and modify the
database schema (tables and constraints)
◆ Data Manipulation Language (DML): To work with the data stored
in the database
SQL is case insensitive, but UPPER-CASE LETTERS are used to write
the language keywords
You can use ; to end a statement (though is not required)

Although SQL has been standardized most database vendors


have introduced changes not supported by the others:
◆ Stay within the standard as much as possible
dit
UPM
Data manipulation
Keywords you should know:
◆ Query
SELECT – Gets records from the database
◆ Modification
UPDATE
DELETE
INSERT INTO – Adds records to the database
◆ Database instance management
CREATE DATABASE – Creates a new instance
ALTER DATABASE – Modifies a database instance
◆ Table management
CREATE TABLE
ALTER TABLE – Modifies a existing table
DROP TABLE – Deletes a table (and all the associated data!!)
dit
UPM
Data manipulation
dit
UPM
Data manipulation
dit
UPM
Running the database
◆ Starting a simple SQL editor online
https://sqliteonline.com/ - PostgreSQL
◆ Setting up a new schema
1. Open apsv.sql (available in Moodle)
2. Copy all its contents
3. Paste it into the SQL console (PostgreSQL)
4. Run the code
dit
UPM
SELECT statement
Get the chosen data from the columns of a table
Syntax:
SELECT col1, col2
FROM table;

Some examples:
◆ SELECT acronym FROM groups;
◆ SELECT name FROM researchers;
◆ SELECT * FROM projects;
◆ SELECT DISTINCT rgroup FROM researchers;
dit
UPM
Built-in functions
Aggregate functions return a single value, calculated from values in
a column
AVG(), COUNT(), MAX(), MIN()…
◆ SELECT COUNT(*) FROM researchers;
◆ SELECT MAX(budget) FROM projects;
◆ SELECT MAX(budget), name FROM projects;

Scalar functions return a single value, based on the input value


UPPER(), LOWER(), INITCAP()…
◆ SELECT UPPER(name) FROM researchers;
dit
UPM
WHERE clause
Filter records, returning only those that fulfil the WHERE criteria
Syntax:
SELECT col1, col2
FROM table
WHERE colx operator value;
Some examples:
◆ SELECT * FROM projects WHERE budget=25000;
◆ SELECT * FROM projects WHERE budget<100000 AND budget>40000;
◆ SELECT * FROM groups WHERE acronym=‘STRAST’;
◆ SELECT * FROM groups WHERE acronym IN (‘STRAST’, ‘GSI’);
◆ SELECT * FROM researchers WHERE name LIKE ‘%Juan%’
% - zero or more characters
_ - one single character
[abc] – any character within the range
[!abc] – any character but those in the range
dit
UPM
Sorting results
It does not change the results, but just how they are presented
Syntax:
SELECT col1, col2
FROM table
ORDER BY colx ASC | DESC
LIMIT 1;

Some examples:
◆ SELECT * FROM projects ORDER BY budget ASC;
◆ SELECT * FROM projects ORDER BY budget DESC;
◆ SELECT * FROM researchers WHERE rgroup=‘STRAST’ ORDER BY name;
◆ SELECT * FROM researchers ORDER BY rgroup, name;
dit
UPM
Querying several tables
SELECT * FROM researchers, groups
WHERE researchers.rgroup=groups.acronym

JOIN is used to combine rows from two or more tables when


there are common fields between them e.g. as for a FK

There are three major types of JOINs:


◆ INNER JOIN: Returns all rows when there is at least one match
in both tables
◆ LEFT JOIN: Return all rows from the left table, and the matched
rows from the right table
◆ RIGHT JOIN: Return all rows from the right table, and the
matched rows from the left table
dit
UPM
INNER JOIN
Returns all rows when there is at least one match in BOTH tables
Syntax:
SELECT table1.col1, table2.col2
FROM table1
INNER JOIN table2
ON table1.colX comp table2.colY;

Examples – Researchers and their groups:


SELECT researchers.name, groups.name
FROM groups
INNER JOIN researchers ON groups.acronym=researchers.rgroup;
dit
UPM
LEFT JOIN
Return all rows from the left table, and the matched rows from the
right table
Syntax:
SELECT table1.col1, table2.col2
FROM table1
LEFT JOIN table2
ON table1.colX comp table2.colY;

Example – All the groups, w/ and wo/ researchers:


SELECT researchers.name, groups.name
FROM groups
LEFT JOIN researchers ON groups.acronym=researchers.rgroup;
dit
UPM
RIGHT JOIN
Return all rows from the right table, and the matched rows from the left
table
Syntax:
SELECT table1.col1, table2.col2
FROM table1
RIGHT JOIN table2
ON table1.colX comp table2.colY;

Example – All the researchers and their groups:


SELECT researchers.name, groups.name
FROM groups
RIGHTJOIN researchers ON groups.acronym=researchers.rgroup;
dit
UPM
Grouping resulting rows by columns
All the results can be groups by one or several columns when they
share the same column value
◆ The HAVING clause filters the resulting groups according to the
criteria
Syntax:
SELECT col1, col2
FROM table
GROUP BY colx
HAVING criteria;

Example – Researchers participating in 2 projects:


SELECT researchers.name, count(*)
FROM researchers
INNER JOIN participants ON researchers.id = participants.researcher
GROUP BY researchers.name
HAVING count(*)>2;
dit
UPM
SQL Subquery (inner/nested query)
◆ A subquery is a SQL query within a query
It can return individual values or a list of
records
It must be enclosed in brackets (…)
Syntax:
SELECT x.cols
FROM (subquery) AS x;
SELECT cols FROM table
WHERE colx operator (subquery);
dit
UPM
INSERT statement
Adds a row to a table
Syntax:
INSERT INTO table
VALUES(val1, val2, val3);

INSERT INTO table(colx, coly)


VALUES(val1, val2);

Example:
◆ INSERT INTO groups(name, acronym)
VALUES(‘Grupo de Sistemas Inteligentes', ’GSI')
dit
UPM
UPDATE statement
Changes the values to one or more columns in one or several tables
◆ The WHERE clause is used to choose the rows to update
◆ If WHERE is not used all rows will be updated

Syntax:
UPDATE table
SET colX=val1, colY=val2
WHERE colZ comp val;

Example:
◆ UPDATE groups
SET acronym=‘gSI’
WHERE acronym=‘GSI’;
dit
UPM
DELETE statement
Deletes rows from a table
◆ The WHERE clause is used to choose the rows to delete
◆ If WHERE is not used all rows will be deleted

Syntax:
DELETE FROM table
WHERE colZ comp val;

Example – Delete a course:


◆ DELETE FROM researchers
WHERE name=‘Juan’;
dit
UPM
Summary and next steps
SQL defines a language to talk to relational databases
◆ SELECT to query the database
Filtering the results and applying functions
In one or several tables
◆ INSERT to add data
◆ UPDATE to modify data
◆ DELETE to remove data

How to go deeper
◆ Details on SQL
http://www.w3schools.com/sql
◆ Details on PostgreSQL
https://www.postgresqltutorial.com/

You might also like