diff --git a/works/lab-01/queries.sql b/works/lab-01/queries.sql index 342deb4..0e586f3 100644 --- a/works/lab-01/queries.sql +++ b/works/lab-01/queries.sql @@ -423,3 +423,48 @@ order by total_cards_used desc, energy_type_name -- Работа (lesson6) +-- Запрос 1 — IS NULL / IS NOT NULL +select c.card_name, pc.pokemon_hp +from pokemon_card pc +left join card c on c.card_id = pc.pokemon_card_id +where pc.pokemon_hp is null +order by c.card_name +-- where pokemon_hp = null всегда ничего не выведет, т.к. сравнение nullа всегда null + +-- Запрос 2 — COALESCE и NULLIF +select c.card_name, coalesce(pc.pokemon_hp, 0) as pokemon_hp, coalesce(pc.pokemon_stage, 'Unknown') as pokemon_stage, COALESCE(NULLIF(pokemon_hp, 0), 1) as hp_or_one +from card c +left join pokemon_card pc on c.card_id = pc.pokemon_card_id +order by c.card_name + +-- Запрос 3 — NULL в агрегатах +select count(*) as pkmn_count, count(pc.pokemon_hp) as hp_count, sum(pc.pokemon_hp) as hp_sum, coalesce(sum(pc.pokemon_hp), 0) as cl_hp_sum, round(avg(pc.pokemon_hp), 2) as avg_hp2 +from pokemon_card pc +-- если не у всех покемонов есть данные о здоровье, то pkmn_count и hp_count различаются. (а ещё если нет покемонов в принципе, то count выдаст всё равно 0, а sum выдаст NULL) +-- если ни у одного покемона нету данных о здоровье, то hp_sum может вернуть null. + +-- Запрос 4 — пустое множество. +select count(*) as pkmn_count, count(pc.pokemon_hp) as hp_count, sum(pc.pokemon_hp) as hp_sum, coalesce(sum(pc.pokemon_hp), 0) as cl_hp_sum, round(avg(pc.pokemon_hp), 2) as avg_hp2 +from pokemon_card pc +where pokemon_hp is null + +-- Запрос 5 — LEFT JOIN + фильтр в WHERE (ошибка) +select c.card_name, c.card_type, pc.pokemon_hp +from card c +left join pokemon_card pc on c.card_id = pc.pokemon_card_id +where pc.pokemon_hp > 80 +-- см. задание 1 (сравнение nullа всегда null) + +-- Запрос 6 — LEFT JOIN + фильтр в ON (исправление) +select c.card_name, c.card_type, pc.pokemon_hp +from card c +left join pokemon_card pc on c.card_id = pc.pokemon_card_id +and pc.pokemon_hp > 80 + +-- Запрос 7 — NULL-safe сравнение через OR + IS NULL +select cs.set_name, cs.set_logo_url, cs.set_symbol_url +from card_set cs +where (set_logo_url = set_symbol_url) or (set_logo_url is null and set_symbol_url is null) + +-- Запрос 8 — NOT IN vs NOT EXISTS +select * from pokemon_card pc \ No newline at end of file