deleted old homework
This commit is contained in:
@@ -1,28 +0,0 @@
|
||||
select * from users
|
||||
|
||||
select id, name, country from users
|
||||
order by name;
|
||||
|
||||
select id, name, country, created_at from users
|
||||
where country = 'KZ'
|
||||
order by created_at desc;
|
||||
|
||||
select id, name from users
|
||||
where email is null;
|
||||
|
||||
select id, name from users
|
||||
where email is not null;
|
||||
|
||||
update users
|
||||
set email = 'alice@mail.com'
|
||||
where name = 'Alice'; -- в моей таблице ни у кого нет email, добавил чтобы посмотреть.
|
||||
|
||||
select id, name, UPPER(email) as email_upper from users;
|
||||
|
||||
select id, name, LOWER(email) as email_lower from users
|
||||
where email is not null;
|
||||
|
||||
select id, name, created_at from users
|
||||
where created_at > '2024-01-11'
|
||||
order by created_at desc
|
||||
limit 2;
|
||||
@@ -1,52 +0,0 @@
|
||||
select * from users
|
||||
|
||||
insert into users (id, name, email, country, created_at)
|
||||
values (1, 'Alice', null, 'KZ', '2026-01-17');
|
||||
|
||||
insert into users (id, name, email, country, created_at)
|
||||
values (2, 'Bob', null, 'USA', '2026-01-17');
|
||||
|
||||
insert into users (id, name, email, country, created_at)
|
||||
values (3, 'Charlie', null, 'RU', '2026-01-17');
|
||||
|
||||
select * from products
|
||||
|
||||
insert into products (id, name, category, price, in_stock)
|
||||
values (1, 'Mi Band 7', 'smartwatch', 49, true)
|
||||
|
||||
insert into products (id, name, category, price, in_stock)
|
||||
values (2, 'Surface Pro', 'tablet', 1299, true)
|
||||
|
||||
insert into products (id, name, category, price, in_stock)
|
||||
values (3, 'Iphone 15', 'smartphone', 999, true)
|
||||
|
||||
insert into products (id, name, category, price, in_stock)
|
||||
values (4, 'Logitech MX Master', 'mouse', 120, true)
|
||||
|
||||
insert into orders (id, user_id, order_date, status)
|
||||
values (1, 1, '2026-01-17', 'in delivery');
|
||||
|
||||
insert into orders (id, user_id, order_date, status)
|
||||
values (2, 1, '2026-01-17', 'shipped');
|
||||
|
||||
insert into orders (id, user_id, order_date, status)
|
||||
values (3, 2, '2026-01-17', 'preparing');
|
||||
|
||||
select * from order_items
|
||||
|
||||
insert into order_items (order_id, product_id, quantity, price_at_purchase)
|
||||
values (1,1, 2, 49)
|
||||
|
||||
insert into order_items (order_id, product_id, quantity, price_at_purchase)
|
||||
values (1, 2, 1, 1299)
|
||||
|
||||
insert into order_items (order_id, product_id, quantity, price_at_purchase)
|
||||
values (2, 3, 1, 999)
|
||||
|
||||
insert into order_items (order_id, product_id, quantity, price_at_purchase)
|
||||
values (3, 4, 2, 120)
|
||||
|
||||
insert into order_items (order_id, product_id, quantity, price_at_purchase)
|
||||
values (3, 1, 2, 49)
|
||||
|
||||
-- Сейчас уже понял, что можно после insert - values добавлять сразу в скобках несколько штук, но изначально делал так.
|
||||
@@ -1,22 +0,0 @@
|
||||
select * from products
|
||||
|
||||
select name, category, price, price * 1.3 as price_with_markup, price * 1.5 as price_with_vat
|
||||
from products
|
||||
where in_stock is true and category != 'smartwatch' and price between 100 and 1300;
|
||||
|
||||
select name, price, price * 0.85 as price_with_discount
|
||||
from products
|
||||
where price < 100 or price > 1000;
|
||||
|
||||
select id, name, category, price from products
|
||||
where category in ('tablet', 'smartphone', 'mouse') and price > 100;
|
||||
|
||||
select name, category, price from products
|
||||
where name not ilike '%iphone%';
|
||||
|
||||
select name, category, price from products
|
||||
where name ilike '%pro%';
|
||||
|
||||
select name, category, price from products
|
||||
where name ilike '%mx%'
|
||||
order by price desc;
|
||||
@@ -1,72 +0,0 @@
|
||||
create table products_extended (
|
||||
id INT primary key,
|
||||
name VARCHAR(50) not null,
|
||||
category VARCHAR(50) not null,
|
||||
price INT,
|
||||
in_stock BOOLEAN default true,
|
||||
email_support VARCHAR(50)
|
||||
);
|
||||
|
||||
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');
|
||||
|
||||
alter table products_extended
|
||||
alter column price type DECIMAL(10, 2)
|
||||
|
||||
------------------ Задание №1 (По нему ты говорил, что нечитаемо, объясни как переделать pls <3)
|
||||
with email_false as (select * from
|
||||
(
|
||||
select category, name, price, email_support,
|
||||
row_number() over (partition by category order by price desc) as rn
|
||||
from products_extended
|
||||
where email_support is null)
|
||||
where rn = 1
|
||||
),
|
||||
|
||||
email_true as (select * from
|
||||
(
|
||||
select category, name, price, email_support,
|
||||
row_number() over (partition by category order by price desc) as rn
|
||||
from products_extended
|
||||
where email_support is not null)
|
||||
where rn = 1
|
||||
)
|
||||
|
||||
select category, name, price, email_support from email_false
|
||||
union all
|
||||
select category, name, price, email_support from email_true
|
||||
order by category, price desc
|
||||
|
||||
---------------------- Задание №2 (Переделал с двух оконок в одном запросе на СТЕ с одной оконкой внутри)
|
||||
with get_data as (
|
||||
select name, category, price,
|
||||
LEAD(price) OVER (PARTITION BY category order by price) AS next_price
|
||||
from products_extended
|
||||
)
|
||||
|
||||
select name, category, price, next_price,
|
||||
price - next_price AS diff
|
||||
from get_data
|
||||
|
||||
--------------------- Задание №3
|
||||
select category, name, price,
|
||||
SUM(price) over (partition by category order by price) as cumulative_sales
|
||||
from products_extended
|
||||
|
||||
---------------------- Задание №4
|
||||
with get_data as (
|
||||
select category, name, price, AVG(price) over (partition by category) as avg_price
|
||||
from products_extended
|
||||
)
|
||||
select category, name, price, avg_price,
|
||||
case when price > avg_price then true else false end as is_above_avg
|
||||
from get_data
|
||||
@@ -1,35 +0,0 @@
|
||||
create table users (
|
||||
id int primary key,
|
||||
name VARCHAR(100) not null,
|
||||
email VARCHAR(100),
|
||||
country VARCHAR(50),
|
||||
crated_at date not null
|
||||
) ;
|
||||
|
||||
create table products (
|
||||
id int primary key,
|
||||
name VARCHAR(100) not null,
|
||||
category VARCHAR(50),
|
||||
price DECIMAL(10, 2),
|
||||
in_stock BOOLEAN default true
|
||||
) ;
|
||||
|
||||
create table orders (
|
||||
id int primary key,
|
||||
user_id int,
|
||||
order_date DATE not null,
|
||||
status VARCHAR(20),
|
||||
foreign key (user_id) references users(id)
|
||||
) ;
|
||||
|
||||
create table order_items (
|
||||
order_id int,
|
||||
product_id int,
|
||||
quantity int not null,
|
||||
price_at_purchase DECIMAL(10, 2) not null,
|
||||
foreign key (order_id) references orders(id),
|
||||
foreign key (product_id) references products(id)
|
||||
) ;
|
||||
|
||||
alter table users
|
||||
rename column crated_at to created_at;
|
||||
@@ -1,23 +0,0 @@
|
||||
create table products_extended (
|
||||
id INT primary key,
|
||||
name VARCHAR(100) not null,
|
||||
category VARCHAR(50),
|
||||
price DECIMAL(10, 2),
|
||||
in_stock BOOLEAN default true,
|
||||
email_support VARCHAR(50)
|
||||
);
|
||||
|
||||
insert into products_extended (id, name, category, price, in_stock, email_support)
|
||||
values (1, 'Mi Band 7', 'smartwatch', 49, true, 'miband@xiaomi.com');
|
||||
|
||||
insert into products_extended (id, name, category, price, in_stock, email_support)
|
||||
values (2, 'Surface Pro', 'tablet', 1299, true, 'surface@microsoft.com');
|
||||
|
||||
select * from products_extended -- проверил как выглядит таблица
|
||||
|
||||
alter table products_extended
|
||||
add column rating DECIMAL(5, 2);
|
||||
|
||||
truncate table products_extended;
|
||||
|
||||
drop table products_extended
|
||||
Reference in New Issue
Block a user