Files
2026-01-14 18:16:52 +03:00

104 lines
3.5 KiB
SQL
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
-- Создание и заполнение таблицы
create table products_extended(
id INT primary key,
name TEXT,
category TEXT,
price DECIMAL,
in_stock BOOLEAN,
email_support TEXT
);
INSERT INTO products_extended (id, name, category, price, in_stock, email_support) VALUES
(1, 'iPhone 14', 'smartphone', 799, TRUE, 'iphone@apple.com'),
(2, 'MacBook Air', 'laptop', 1199, FALSE, NULL),
(3, 'Galaxy S22', 'smartphone', 699, TRUE, 'galaxy@support.com'),
(4, 'ThinkPad X1', 'laptop', 1399, TRUE, 'thinkpad@lenovo.com'),
(5, 'iPad Pro', 'tablet', 999, FALSE, 'ipad@apple.com'),
(6, 'Kindle Paperwhite', 'ebook_reader', 149, TRUE, NULL),
(7, 'Mi Band 7', 'smartwatch', 49, TRUE, 'miband@xiaomi.com'),
(8, 'Surface Pro', 'tablet', 1299, TRUE, 'surface@microsoft.com'),
(9, 'Honor Pad X9', 'tablet', 219, TRUE, 'honor@support.com'),
(10, 'Dell XPS 13', 'laptop', 1099, FALSE, 'dell@support.com');
/*
Задача 1:
Выведи все товары, которые:
- в наличии
- НЕ относятся к `smartwatch` и `ebook_reader`
- стоят от 300 до 1300
И для них посчитай:
1. price с наценкой 10%
2. price с НДС 20%
3. Статус: если email_support IS NULL, подставь нет почты, иначе — сам email
*/
select name, category, price, price * 1.1 as price_with_markup, price * 1.2 as price_with_vat, coalesce(email_support, 'нет почты') as status
from products_extended
where in_stock = true and category not in ('smartwatch', 'ebookreader') and price between 300 and 1300;
/*
Задача 2:
Найти все товары:
1. У которых цена меньше 150 или больше 1200
2. У которых** есть email поддержки
Для каждого выведи:
1. Название
2. Категорию
3. Email
4. Цену с прибавкой 25%
5. Отметку `premium` — если цена > 1000
*/
select name, category, email_support, price * 1.25 as price_25, (price> 1000) as premium
from products_extended
where (price < 150 or price > 1200) and email_support is not null
order by premium;
/*
Задача 3:
Найди все товары:
- где категория — `smartphone`, `laptop`, `tablet`
- название НЕ содержит `'i'` (чтобы исключить Apple)
- цена от 500 до 1300
Выведи:
- `id`, `name`, `category`, `price`
- отсортируй по `id`
*/
select id, name, category, price
from products_extended
where category in('smartphone', 'laptop', 'tablet') and name not like '%i%' and price between 500 and 1300
order by id desc;
/*
Задача 4:
Найди товары, у которых:
- email_support IS NULL
- они в наличии
- стоят менее 800
Выведи:
- `name`, `category`, `price`
- добавь колонку `comment`: `'нет поддержки'`
*/
select name, category, price, coalesce (email_support, 'нет поддержки') as comment
from products_extended
where email_support is null and in_stock is not null and price < 800;
/*
Задача 5:
Найди товары, у которых:
- название заканчивается на `'Pro'` ИЛИ содержит `'X'` внутри (например, `X1`, `XPS`)
- в наличии
- с ценой > 700
Выведи:
- `name`, `category`, `price`
- флаг `premium` — `true`, если `price > 1000`, иначе `false`
*/
select name, category, price, (price>1000) as premium
from products_extended
where (name like '%Pro%' or name like '%X%') and in_stock is true and price > 700
order by price desc;