From 15e6a541d106c538ca2730587c9a0d80789249c8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=90=D0=BD=D0=B4=D1=80=D0=B5=D0=B9=20=D0=9C=D0=B0=D1=85?= =?UTF-8?q?=D0=BE=D1=82=D0=BA=D0=B8=D0=BD?= <16+andrix7777777@noreply.localhost> Date: Mon, 26 Oct 2026 10:30:39 +0000 Subject: [PATCH] =?UTF-8?q?=D0=9E=D0=B1=D0=BD=D0=BE=D0=B2=D0=B8=D1=82?= =?UTF-8?q?=D1=8C=20works/lab-01/queries.sql?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- works/lab-01/queries.sql | 45 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) 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