Advertisement
Guest User

Untitled

a guest
Feb 15th, 2017
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.32 KB | None | 0 0
  1. 1.1. UPDATE Presidents SET LastName='Roosevelt' WHERE LastName LIKE 'Franklin';
  2.  
  3. 1.2. SELECT * FROM Presidents WHERE (LastName >= 'Franklin' AND LastName <= 'Roosevelt')
  4.  
  5. 1.3. select distinct salary from Salaries order by salary DESC OFFSET 9 LIMIT 1;
  6.  
  7.  
  8. 2.1
  9.  
  10. # Create database
  11.  
  12. CREATE DATABASE sql_task;
  13. \connect sql_task
  14.  
  15.  
  16. # CREATE Tables
  17.  
  18. CREATE TABLE Tags (
  19. discipline_tag VARCHAR(40) UNIQUE
  20. );
  21.  
  22.  
  23. CREATE TABLE Persons (
  24. person_id serial NOT NULL UNIQUE,
  25. first_name varchar(40) NOT NULL,
  26. last_name varchar(40) NOT NULL,
  27. email varchar(80) NOT NULL UNIQUE,
  28. PRIMARY KEY (person_id)
  29. );
  30.  
  31. CREATE TABLE Courses (
  32. course_id serial NOT NULL UNIQUE,
  33. course_name VARCHAR(90) NOT NULL,
  34. course_tag VARCHAR(40) REFERENCES Tags(discipline_tag),
  35. start_date DATE,
  36. end_date DATE
  37. );
  38.  
  39. CREATE TABLE person_course_join_table (
  40. person INT REFERENCES Persons(person_id),
  41. course INT REFERENCES Courses(course_id),
  42. UNIQUE (person, course)
  43. );
  44.  
  45.  
  46. # Populate some data
  47.  
  48. INSERT INTO Tags (discipline_tag) VALUES ('Math');
  49. INSERT INTO Tags (discipline_tag) VALUES ('Geography');
  50. INSERT INTO Tags (discipline_tag) VALUES ('History');
  51. INSERT INTO Tags (discipline_tag) VALUES ('Physics');
  52. INSERT INTO Tags (discipline_tag) VALUES ('Literature');
  53. INSERT INTO Tags (discipline_tag) VALUES ('Design');
  54.  
  55.  
  56. INSERT INTO Persons (first_name, last_name, email) VALUES ('Anthony','Freeman','anthony@maytech.net');
  57. INSERT INTO Persons (first_name, last_name, email) VALUES ('Emilia','Prygrodska','mila@rambler.ru');
  58. INSERT INTO Persons (first_name, last_name, email) VALUES ('Olga','Mlynko','mlynkolga@gmail.com');
  59. INSERT INTO Persons (first_name, last_name, email) VALUES ('Taras','Kozak','tarskozak@gmail.com');
  60. INSERT INTO Persons (first_name, last_name, email) VALUES ('Oleg','Doskach','oleg45d@gmail.com');
  61. INSERT INTO Persons (first_name, last_name, email) VALUES ('Taras','Sosnovskyi','taras@comfort-service.com');
  62.  
  63.  
  64. INSERT INTO Courses (course_name, course_tag, start_date, end_date) VALUES ('Addictive Math','Math','2016-10-21','2016-11-02');
  65. INSERT INTO Courses (course_name, course_tag, start_date, end_date) VALUES ('Physics around us','Physics','2016-11-01','2016-11-30');
  66. INSERT INTO Courses (course_name, course_tag, start_date, end_date) VALUES ('Fashion Design','Design','2016-11-12','2016-11-30');
  67. INSERT INTO Courses (course_name, course_tag, start_date, end_date) VALUES ('WEB Design','Design','2017-01-01','2017-02-16');
  68.  
  69. INSERT INTO person_course_join_table (person, course) VALUES ((SELECT person_id FROM Persons LIMIT 1), (SELECT course_id FROM Courses LIMIT 1) );
  70. INSERT INTO person_course_join_table (person, course) VALUES ((SELECT person_id FROM Persons LIMIT 1 OFFSET 1), (SELECT course_id FROM Courses LIMIT 1 OFFSET 1) );
  71. INSERT INTO person_course_join_table (person, course) VALUES ((SELECT person_id FROM Persons LIMIT 1 OFFSET 2), (SELECT course_id FROM Courses LIMIT 1 OFFSET 2) );
  72. INSERT INTO person_course_join_table (person, course) VALUES ((SELECT person_id FROM Persons LIMIT 1 OFFSET 3), (SELECT course_id FROM Courses LIMIT 1 OFFSET 3) );
  73.  
  74.  
  75.  
  76. # QUERYING
  77.  
  78. a) SELECT course_name, course_tag FROM Courses;
  79.  
  80. course_name | course_tag
  81. -------------------+---------
  82. Addictive Math | Math
  83. Physics around us | Physics
  84. Fashion Design | Design
  85. WEB Design | Design
  86. (4 rows)
  87.  
  88.  
  89. b) SELECT first_name,Last_name,email FROM Persons LEFT JOIN person_course_join_table ON Persons.person_id=person_course_join_table.person where person_course_join_table.person IS NULL;
  90.  
  91. first_name | last_name | email
  92. ------------+------------+---------------------------
  93. Oleg | Doskach | oleg45d@gmail.com
  94. Taras | Sosnovskyi | taras@comfort-service.com
  95. (2 rows)
  96.  
  97.  
  98. c) SELECT * FROM Tags WHERE discipline_tag IN (SELECT course_tag From Courses);
  99.  
  100. discipline_tag
  101. ---------
  102. Math
  103. Physics
  104. Design
  105. (3 rows)
  106.  
  107.  
  108. d) SELECT EXISTS (SELECT 1 From Courses WHERE course_tag='Design');
  109.  
  110. bool
  111. ------
  112. t
  113. (1 row)
  114.  
  115.  
  116. e) SELECT course_name FROM Courses WHERE EXTRACT(WEEK FROM end_date) = EXTRACT(WEEK FROM NOW());
  117.  
  118. course_name
  119. ----------------
  120. Addictive Math
  121. (1 row)
  122.  
  123.  
  124. f) SELECT * FROM Courses ORDER BY (end_date - start_date) DESC LIMIT 1;
  125.  
  126. person_id | course_name | course_tag | start_date | end_date
  127. ------+------------+--------+------------+------------
  128. 4 | WEB Design | Design | 2017-01-01 | 2017-02-16
  129. (1 row)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement