Advertisement
markusq

MBX Task #2

Mar 16th, 2023 (edited)
1,023
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* The request code for task 2 is below. Used PostgreSQL */
  2.  
  3. SELECT p.ProductName AS ProductName, c.CategoryName AS CategoryName
  4. FROM Product p
  5. LEFT JOIN ProductCategory pc ON pc.ProductId = p.ProductId
  6. LEFT JOIN Category c ON c.CategoryId = pc.CategoryId;
Tags: sql
Advertisement
Comments
  • markusq
    2 years (edited)
    1. /* Below is the code for creating and filling the database: */
    2.  
    3. create table Product
    4. (
    5.     ProductId int primary key,
    6.     ProductName varchar(128) not null
    7. )
    8.  
    9. insert into Product(ProductId, ProductName) values (1, N'Acer K222HQL')
    10. insert into Product(ProductId, ProductName) values (2, N'Intel Core i5-10400')
    11. insert into Product(ProductId, ProductName) values (3, N'Samsung M378A1K43EB2-CWE')
    12. insert into Product(ProductId, ProductName) values (4, N'Logitech M590')
    13.  
    14. create table Category
    15. (
    16.     CategoryId int primary key,
    17.     CategoryName varchar(64) not null
    18. )
    19.  
    20. insert into Category(CategoryId, CategoryName) values(1, N'CPU')
    21. insert into Category(CategoryId, CategoryName) values(2, N'RAM')
    22. insert into Category(CategoryId, CategoryName) values(3, N'Monitor')
    23.  
    24. create table ProductCategory
    25. (
    26.     CategoryId int references Category(CategoryId),
    27.     ProductId int references Product(ProductId),
    28.     primary key(CategoryId, ProductId)
    29. )
    30.  
    31. insert into ProductCategory(CategoryId, ProductId) values (3, 1)
    32. insert into ProductCategory(CategoryId, ProductId) values (1, 2)
    33. insert into ProductCategory(CategoryId, ProductId) values (2, 3)
Add Comment
Please, Sign In to add comment
Advertisement