Advertisement
CastelShal

DBMS Prac 2

Aug 25th, 2023 (edited)
595
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
T-SQL 5.02 KB | None | 0 0
  1. create database shalPrac
  2. use shalPrac
  3.  
  4. -- Create Customers Table
  5. CREATE TABLE Customers (
  6.     customer_id INT PRIMARY KEY,
  7.     customer_name VARCHAR(255),
  8.     email VARCHAR(255),
  9.     phone_number VARCHAR(15)
  10. );
  11.  
  12. -- Insert Data into Customers Table
  13. INSERT INTO Customers (customer_id, customer_name, email, phone_number)
  14. VALUES
  15.     (1, 'John Smith', 'john.smith@example.com', '123-456-7890'),
  16.     (2, 'Jane Doe', 'jane.doe@example.com', '987-654-3210'),
  17.     (3, 'Alex Johnson', 'alex.johnson@example.com', '555-555-5555'),
  18.     (4, 'Emily Brown', 'emily.brown@example.com', '444-444-4444'),
  19.     (5, 'Michael Lee', 'michael.lee@example.com', '333-333-3333');
  20.  
  21. -- Create Products Table
  22. CREATE TABLE Products (
  23.     product_id INT PRIMARY KEY,
  24.     product_name VARCHAR(255),
  25.     category VARCHAR(255),
  26.     price DECIMAL(10, 2)
  27. );
  28.  
  29. -- Insert Data into Products Table
  30. INSERT INTO Products (product_id, product_name, category, price)
  31. VALUES
  32.     (101, 'Laptop', 'Electronics', 800.00),
  33.     (102, 'T-Shirt', 'Clothing', 20.00),
  34.     (103, 'Book', 'Books', 15.00),
  35.     (104, 'Smartphone', 'Electronics', 600.00),
  36.     (105, 'Sneakers', 'Shoes', 80.00);
  37.  
  38. -- Create Orders Table
  39. CREATE TABLE Orders (
  40.     order_id INT PRIMARY KEY,
  41.     customer_id INT,
  42.     order_date DATE,
  43.     FOREIGN KEY (customer_id) REFERENCES Customers(customer_id)
  44. );
  45.  
  46. -- Insert Data into Orders Table
  47. INSERT INTO Orders (order_id, customer_id, order_date)
  48. VALUES
  49.     (1, 1, '2023-01-15'),
  50.     (2, 2, '2023-02-01'),
  51.     (3, 3, '2023-03-10'),
  52.     (4, 4, '2023-03-25'),
  53.     (5, 5, '2023-04-05');
  54.  
  55. -- Create Order_Details Table
  56. CREATE TABLE Order_Details (
  57.     order_detail_id INT PRIMARY KEY,
  58.     order_id INT,
  59.     product_id INT,
  60.     quantity INT,
  61.     FOREIGN KEY (order_id) REFERENCES Orders(order_id),
  62.     FOREIGN KEY (product_id) REFERENCES Products(product_id)
  63. );
  64.  
  65. -- Insert Data into Order_Details Table
  66. INSERT INTO Order_Details (order_detail_id, order_id, product_id, quantity)
  67. VALUES
  68.     (1, 1, 101, 2),
  69.     (2, 1, 103, 5),
  70.     (3, 2, 102, 3),
  71.     (4, 3, 104, 1),
  72.     (5, 4, 101, 4);
  73.  
  74. -- Create Reviews Table
  75. CREATE TABLE Reviews (
  76.     review_id INT PRIMARY KEY,
  77.     product_id INT,
  78.     customer_id INT,
  79.     rating INT,
  80.     comment TEXT,
  81.     FOREIGN KEY (product_id) REFERENCES Products(product_id),
  82.     FOREIGN KEY (customer_id) REFERENCES Customers(customer_id)
  83. );
  84.  
  85. -- Insert Data into Reviews Table
  86. INSERT INTO Reviews (review_id, product_id, customer_id, rating, comment)
  87. VALUES
  88.     (1, 101, 1, 4, 'Great laptop!'),
  89.     (2, 102, 2, 5, 'Love the T-shirt!'),
  90.     (3, 101, 3, 3, 'Decent laptop.'),
  91.     (4, 104, 4, 5, 'Excellent smartphone.'),
  92.     (5, 101, 5, 4, 'Good laptop for the price');
  93.  
  94. SELECT
  95.   *
  96. FROM
  97.   INFORMATION_SCHEMA.TABLES;
  98. GO
  99.  
  100. select * from Customers
  101. select * from Order_Details
  102. select * from Orders
  103. select * from Products
  104. select * from Reviews
  105. select * from Orders,Order_Details
  106.     where Order_Details.order_id = Orders.order_id
  107.  
  108. --1
  109. select Customers.customer_name from Orders, Customers
  110.         where Orders.customer_id = Customers.customer_id and
  111.         order_id in (select top 5 order_id from Order_Details
  112.                 group by order_id
  113.                 order by count(order_id) desc)
  114.  
  115. --2
  116. select Products.product_name, avg(rating) from Reviews, Products
  117.     where Reviews.product_id = Products.product_id
  118.     group by product_name
  119.     order by avg(rating) desc;
  120.  
  121. --3
  122. select sum(price * quantity) as Total from Order_Details od, Products p, Orders o
  123.     where od.product_id = p.product_id and o.order_id = od.order_id and year(o.order_date) = 2023
  124.  
  125. --4
  126. with sq as
  127. (select o.customer_id, (p.price * od.quantity) "Sub"
  128.     from Products p, Order_Details od, Orders o
  129.     where p.product_id = od.product_id
  130.     and o.order_id = od.order_id
  131.     )
  132. select sq.customer_id from sq
  133.     group by sq.customer_id
  134.     having sum(Sub) >= ALL (Select sum(s.sub) from sq s group by s.customer_id)
  135.    
  136. --5
  137. select top 1 category, sum(price * quantity) "TotalSales" from Order_Details od, Products p, Orders o
  138.     where od.product_id = p.product_id
  139.     and o.order_id = od.order_id
  140.     and year(o.order_date) = 2023
  141.     group by category
  142.     order by TotalSales desc
  143.  
  144. --6
  145. select order_id, avg(quantity) from Order_Details
  146.     group by order_id
  147.  
  148. --7
  149. select product_name from Products
  150.     where Products.product_id not in
  151.     (select product_id from Reviews)
  152.  
  153. --8
  154. select customer_id, count(customer_id) "count" from Reviews
  155.     group by customer_id
  156.     having count(customer_id) >= ALL (select count(customer_id) "counts" from Reviews
  157.                             group by customer_id)
  158.    
  159. --9
  160. select category, sum(price * quantity) "TotalSales"
  161.     from Order_Details od, Products p, Orders o
  162.     where od.product_id = p.product_id
  163.     and o.order_id = od.order_id
  164.     group by category
  165.  
  166. --10
  167. select p.category, p.product_name, maxprice
  168.     from Products p, Order_Details o,
  169.     (select category, max(p.price * od.quantity) "maxprice"
  170.         from Order_Details od, Products p
  171.         where od.product_id = p.product_id
  172.         group by category)
  173.         as maxtable
  174.     where (p.price * o.quantity) = maxtable.maxprice
  175.     and p.category = maxtable.category
  176.    
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement