Skip to content

Instantly share code, notes, and snippets.

@SlavikArt
Created January 21, 2024 16:08
Show Gist options
  • Save SlavikArt/c8ddba3d20e9fdcf351a2b6cfd82ec89 to your computer and use it in GitHub Desktop.
Save SlavikArt/c8ddba3d20e9fdcf351a2b6cfd82ec89 to your computer and use it in GitHub Desktop.

Outer Join Querries (on Product)

Outer_Join

-- 1. Показать названия товаров и их производителей,
-- но и в том числе тех производителей,
-- у которых нет товаров (OUTER JOIN)
SELECT
Product.name AS 'product',
Producer.name AS 'producer'
FROM Product
RIGHT JOIN Producer ON Product.id_producer = Producer.id
-- 2. Показать только те категории,
-- к которым не относится ни один товар (OUTER JOIN)
SELECT
Category.name AS 'category',
Product.name AS 'product'
FROM Category
LEFT JOIN Product ON Category.id = Product.id_category
WHERE Product.id IS NULL
-- 3. Показать области (регионы),
-- в которых нет ни одного производителя (EXCEPT)
SELECT
Region.name AS 'region_name'
FROM Region
EXCEPT
SELECT
Region.name
FROM Producer
JOIN Address ON Producer.id_address = Address.id
JOIN City ON Address.id_city = City.id
JOIN Region ON City.id_region = Region.id
-- 4. Показать те названия категорий,
-- где нет товаров фирмы OOO «Nly u» (EXCEPT)
SELECT
Category.name
FROM Category
EXCEPT
SELECT
Category.name
FROM Product
JOIN Category ON Product.id_category = Category.id
JOIN Producer ON Product.id_producer = Producer.id
WHERE Producer.name = 'OOO Nly u'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment