Advertisement
horozov86

exam preparation 2

Oct 12th, 2023 (edited)
1,326
1
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. 2. INSERT
  2.  
  3. INSERT INTO clients (full_name, phone_number)
  4. SELECT
  5.     CONCAT(first_name, ' ', last_name),
  6.     '(088) 9999'||id*2
  7. FROM
  8.     drivers
  9. WHERE
  10.     id BETWEEN 10 AND 20
  11.  
  12.  
  13. 3.UPDATE
  14.  
  15. UPDATE cars
  16. SET condition = 'C'
  17. WHERE
  18.     (mileage >= 800000 OR mileage IS NULL)
  19.         AND
  20.     year <= 2010
  21.         AND
  22.     make <> 'Mercedes-Benz';
  23.  
  24.  
  25. 4.DELETE
  26.  
  27. DELETE FROM clients
  28. WHERE
  29.     LENGTH(full_name) >= 3
  30.         AND
  31.     id NOT IN (SELECT client_id FROM courses);
  32.  
  33.  
  34. 6.CARS (many to many)
  35.  
  36. SELECT
  37.     d.first_name,
  38.     d.last_name,
  39.     c.make,
  40.     c.model,
  41.     c.mileage
  42. FROM
  43.     drivers AS d
  44. JOIN cars_drivers AS cd
  45. ON cd.driver_id = d.id
  46. JOIN cars AS c
  47. ON c.id = cd.car_id
  48.  
  49. WHERE
  50.     c.mileage IS NOT NULL
  51. ORDER BY
  52.     c.mileage DESC,
  53.     d.first_name;
  54.  
  55.  
  56. 7.Number of Courses for Each Car (броят на курсовете се опрделя, като се COUNT id-тата на courses!!! Използваме LEFT JOIN за да се извлече информация и там където курсовете са 0, съответно average_bill е NULL!!! Използваме HAVING понеже exclude е след агрегациите!!!
  57. SELECT
  58.     cr.id AS car_id,
  59.     cr.make AS make,
  60.     cr.mileage AS mileage,
  61.     COUNT(co.id) AS count_of_courses,
  62.     ROUND(AVG(co.bill), 2) AS average_bill
  63. FROM
  64.     cars AS cr
  65. LEFT JOIN
  66.     courses AS co
  67. ON
  68.     cr.id = co.car_id
  69. GROUP BY
  70.     cr.id,
  71.     cr.make AS make,
  72.     cr.mileage AS mileage
  73. HAVING
  74.     COUNT(co.id) <> 2
  75. ORDER BY
  76.     count_of_courses DESC,
  77.     cr.id ASC;
  78.  
  79.  
  80. 8.Regular Clients - свързват се само таблиците courses и clients, като броя на колите се взима чрез car_id. ПО УСЛОВИЕ КЛИЕНТИТЕ ТРЯБВА ДА СА ПЪТУВАЛ С ПОВЕЧЕ ОТ ЕДНА КОЛАТА, ЗАТОВА ИЗПОЛЗВАМЕ HAVING
  81. SELECT
  82.     cl.full_name,
  83.     COUNT(car_id) AS count_of_cars,
  84.     SUM(co.bill) AS total_sum
  85. FROM
  86.     clients AS cl
  87. JOIN courses AS co
  88.     ON co.client_id = cl.id
  89. WHERE
  90.     cl.full_name LIKE '_a%'
  91. GROUP BY
  92.     cl.full_name
  93. HAVING
  94.     COUNT(car_id) > 1
  95. ORDER BY
  96.     cl.full_name;
  97.  
  98.  
  99. 9.Full Information of Courses - ОСОБЕНО Е CASE END-a!!!
  100.  
  101. SELECT
  102.     a.name,
  103.     CASE
  104.         WHEN EXTRACT(HOUR FROM co.start) BETWEEN 6 AND 20 THEN 'Day'
  105.         ELSE 'Night'   
  106.     END AS day_time,
  107.     co.bill,
  108.     cl.full_name,
  109.     c.make,
  110.     c.model,
  111.     cat.name
  112. FROM
  113.     addresses AS a
  114. JOIN
  115.     courses AS co
  116. ON co.from_address_id = a.id
  117. JOIN
  118.     clients AS cl
  119. ON cl.id = co.client_id
  120. JOIN
  121.     cars AS c
  122. ON c.id = co.car_id
  123. JOIN
  124.     categories AS cat
  125. ON cat.id = c.category_id
  126. ORDER BY
  127.     co.id;
  128.  
  129.  
  130. 10.Find all Courses by Client’s Phone Number
  131.  
  132. CREATE OR REPLACE FUNCTION fn_courses_by_client(phone_num VARCHAR(20))
  133. RETURNS INT
  134. AS
  135. $$
  136. BEGIN
  137.     RETURN (SELECT
  138.                 COUNT(co.id)
  139.             FROM
  140.             clients AS cl
  141.             JOIN courses AS co
  142.             ON co.client_id = cl.id
  143.             WHERE cl.phone_number = phone_num
  144.     );
  145.    
  146. END;
  147. $$
  148. LANGUAGE plpgsql;
  149.  
  150. 11. Full Info for Address
  151.  
  152. CREATE TABLE search_results (
  153.     id SERIAL PRIMARY KEY,
  154.     address_name VARCHAR(50),
  155.     full_name VARCHAR(100),
  156.     level_of_bill VARCHAR(20),
  157.     make VARCHAR(30),
  158.     condition CHAR(1),
  159.     category_name VARCHAR(50)
  160. );
  161.  
  162. CREATE OR REPLACE PROCEDURE sp_courses_by_address(address_name VARCHAR(100))
  163. AS
  164. $$
  165. BEGIN
  166.     TRUNCATE search_results;
  167.     INSERT INTO search_results (address_name, full_name, level_of_bill, make, condition, category_name)
  168.     SELECT
  169.         a.name,
  170.         cl.full_name,
  171.         CASE
  172.             WHEN co.bill <= 20 THEN 'Low'
  173.             WHEN co.bill <= 30 THEN 'Medium'
  174.             ELSE 'High'
  175.         END AS level_of_bill,
  176.         c.make,
  177.         c.condition,
  178.         cat.name
  179.     FROM
  180.         addresses AS a
  181.     JOIN
  182.         courses AS co
  183.     ON co.from_address_id = a.id
  184.     JOIN
  185.         clients AS cl
  186.     ON cl.id = co.client_id
  187.     JOIN
  188.         cars AS c
  189.     ON c.id = co.car_id
  190.     JOIN
  191.         categories AS cat
  192.     ON cat.id = c.category_id
  193.     WHERE
  194.         a.name = address_name
  195.     ORDER BY
  196.         c.make,
  197.         cl.full_name;
  198.        
  199. END;
  200. $$
  201. LANGUAGE plpgsql;
  202.  
  203.  
  204.  
  205.  
  206.  
  207.  
  208.  
  209.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement