From aef8bfe76eb6236929ddfef19d4b7d41ff91e9c6 Mon Sep 17 00:00:00 2001 From: Tomas Krejci Date: Mon, 8 Jun 2026 17:01:47 +0200 Subject: [PATCH] Add second lecture SQL examples --- Lecture_1/01-select.sql | 48 +++++++++++++++++++++ lecture_2/01-definicni-prikazy.sql | 42 ++++++++++++++++++ lecture_2/02-manipulacni-prikazy.sql | 64 ++++++++++++++++++++++++++++ 3 files changed, 154 insertions(+) create mode 100644 lecture_2/01-definicni-prikazy.sql create mode 100644 lecture_2/02-manipulacni-prikazy.sql diff --git a/Lecture_1/01-select.sql b/Lecture_1/01-select.sql index 1505411..726ad2e 100644 --- a/Lecture_1/01-select.sql +++ b/Lecture_1/01-select.sql @@ -30,4 +30,52 @@ SELECT * FROM accounts WHERE currency NOT IN ('CZK', 'USD'); SELECT * FROM clients WHERE email IS NULL; +SELECT * FROM clients WHERE email IS NOT NULL; + SELECT * FROM clients WHERE name LIKE 'Ja%'; + +-- Use ILIKE for a case-insensitive pattern match. +SELECT * FROM clients WHERE name ILIKE 'JA%'; + +-- Sort accounts by balance in ascending or descending order. +SELECT * FROM accounts ORDER BY balance; + +SELECT * FROM accounts ORDER BY balance DESC; + +-- Apply secondary sorting when currency values are equal. +SELECT * FROM accounts ORDER BY currency DESC, balance ASC; + +-- Limit the number of returned records. +SELECT * FROM accounts ORDER BY currency DESC, balance ASC LIMIT 5; + +-- Skip records before returning the limited result set. +SELECT * +FROM accounts +ORDER BY currency DESC, balance ASC +LIMIT 5 +OFFSET + 2; + +-- Return distinct currency values without duplicates. +SELECT DISTINCT (currency) FROM accounts; + +SELECT * FROM cards; + +-- Return cards with their associated clients. +SELECT * FROM cards JOIN clients ON cards.client_id = clients.id; + +-- Select only the required columns from joined tables. +SELECT clients.name, cards.card_number +FROM cards + JOIN clients ON cards.client_id = clients.id; + +-- Use aliases to distinguish similarly named columns. +SELECT clients.id AS client_id, cards.id AS card_id +FROM cards + JOIN clients ON cards.client_id = clients.id; + +SELECT * FROM accounts JOIN cards ON accounts.id = cards.account_id; + +SELECT * FROM accounts LEFT JOIN cards ON accounts.id = cards.account_id; + +-- PostgreSQL console command for describing the accounts table: \d accounts diff --git a/lecture_2/01-definicni-prikazy.sql b/lecture_2/01-definicni-prikazy.sql new file mode 100644 index 0000000..d2f2471 --- /dev/null +++ b/lecture_2/01-definicni-prikazy.sql @@ -0,0 +1,42 @@ +-- Display the name of the current database. + +SELECT CURRENT_DATABASE(); + +-- Create tables starting with entities that do not depend on foreign keys. + +CREATE TABLE enclosures ( + id INT GENERATED ALWAYS AS IDENTITY PRIMARY KEY, + name VARCHAR(255) NOT NULL, + area INTEGER, + biome VARCHAR(20) +); + +-- Delete a table. +DROP TABLE enclosures; + +CREATE TABLE keepers ( + id INT GENERATED ALWAYS AS IDENTITY PRIMARY KEY, + name VARCHAR NOT NULL, + salary NUMERIC(10, 3) +); + +CREATE TABLE animals ( + id INT GENERATED ALWAYS AS IDENTITY PRIMARY KEY, + enclosure_id INT, + name VARCHAR NOT NULL, + is_healthy BOOLEAN DEFAULT TRUE, + adoption_date DATE, + note TEXT, + FOREIGN KEY (enclosure_id) REFERENCES enclosures (id) +); + +-- Junction table for the many-to-many relationship. +CREATE TABLE animals_keepers ( + animal_id INT NOT NULL, + keeper_id INT NOT NULL, + PRIMARY KEY (animal_id, keeper_id), + FOREIGN KEY (animal_id) REFERENCES animals (id), + FOREIGN KEY (keeper_id) REFERENCES keepers (id) +); + +-- Change an existing table structure with ALTER TABLE. diff --git a/lecture_2/02-manipulacni-prikazy.sql b/lecture_2/02-manipulacni-prikazy.sql new file mode 100644 index 0000000..79fd05b --- /dev/null +++ b/lecture_2/02-manipulacni-prikazy.sql @@ -0,0 +1,64 @@ +-- Use INSERT to add a record. +INSERT INTO + enclosures (biome, name, area) +VALUES ( + 'Arktida', + 'Ledové království', + 2500 + ); + +SELECT * FROM enclosures; + +INSERT INTO + enclosures (name, area, biome) +VALUES ( + 'Savanvoá pláň', + 1700, + 'Savana' + ), + ( + 'Křišťálová laguna', + 900, + 'Tropické vody' + ), + ( + 'Zelený divoký les', + 750, + 'Mírný les' + ); + +-- Increase every enclosure area by 100 square meters. +UPDATE enclosures SET area = area + 100; + +SELECT * FROM enclosures; + +-- Increase smaller enclosure areas by 50 square meters. + +UPDATE enclosures SET area = area + 50 WHERE area < 1500; + +-- Update multiple columns in one statement. +UPDATE enclosures SET biome = 'Oceán', area = area + 25 WHERE id = 3; + +-- Delete a selected record. + +DELETE FROM enclosures WHERE id = 3; + +SELECT * FROM enclosures; + +INSERT INTO + animals (name, adoption_date, note) +VALUES ( + 'hroch', + '2026-10-25', + 'hrošík Igor' + ); + +INSERT INTO + animals (name, adoption_date, note) +VALUES ('vlk', NOW(), 'pozor kouše'); + +INSERT INTO + animals (name, adoption_date, note) +VALUES ('vlk', NOW(), 'pozor kouše'); + +SELECT * FROM animals;