-- First lecture SELECT examples.
-- Run 01-bankovnictvi-import.sql before executing these queries.

-- This query returns all columns from the accounts table.
SELECT * FROM accounts;

-- Select only the account number and bank identifier.
SELECT account_number, bank_id FROM accounts;

-- Use WHERE to filter specific records.
SELECT * FROM accounts WHERE currency = 'CZK';

SELECT * FROM accounts WHERE balance > 1000;

SELECT * FROM accounts WHERE balance < 1000 AND currency = 'CZK';

SELECT * FROM accounts WHERE balance > 1000 AND currency = 'CZK';

SELECT *
FROM accounts
WHERE (
        currency = 'USD'
        OR currency = 'EUR'
    )
    and balance < 1000;

SELECT * FROM accounts WHERE currency IN ('CZK', 'USD');

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
