Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 1.
- hotel=# SELECT * FROM guest LEFT OUTER JOIN booking ON guest.phone_number = booking.guest WHERE booking.guest IS NULL;
- first_name | last_name | phone_number | email | address | check_in | check_out | guest | room
- ------------+-----------+--------------+-----------------------+-------------------------------------+----------+-----------+-------+------
- Marcus | Cruz | 6304468851 | rgarcia@optonline.net | 103 Water Ave. Grand Forks ND 58201 | | | |
- Thalia | Cobb | 2269062721 | mxiao@yahoo.com | 8929 Hall Court Flushing NY 11354 | | | |
- (2 rows)
- ^
- hotel=# SELECT guest.first_name, guest.last_name, booking.check_in, COUNT(booking.check_in) FROM guest JOIN booking ON guest.phone_number = booking.guest GROUP BY guest.first_name, guest.last_name, booking.check_in HAVING COUNT(*) > 1;
- first_name | last_name | check_in | count
- ------------+-----------+------------+-------
- Waylond | Dalton | 2017-10-22 | 3
- (1 row)
- hotel=# SELECT * FROM booking ou JOIN guest ON ou.guest = guest.phone_number WHERE(SELECT count(*) from booking inr WHERE inr.room = ou.room AND inr.guest = ou.guest) > 1;
- check_in | check_out | guest | room | first_name | last_name | phone_number | email | address
- ------------+------------+------------+------+------------+-----------+--------------+---------------------+---------------------------------------
- 2017-11-10 | 2017-11-11 | 1255464478 | 9 | Justine | Henderson | 1255464478 | fangorn@hotmail.com | 7591 Pheasant St. Schaumburg IL 60193
- 2017-11-13 | 2017-11-14 | 1255464478 | 9 | Justine | Henderson | 1255464478 | fangorn@hotmail.com | 7591 Pheasant St. Schaumburg IL 60193
- 2017-11-15 | 2017-11-16 | 1255464478 | 9 | Justine | Henderson | 1255464478 | fangorn@hotmail.com | 7591 Pheasant St. Schaumburg IL 60193
- (3 rows)
- 2.
- I will use three tables, one for students one for grades and one for classes.
- Student table will hold the name, id, phone number address and other personal info on each student.
- The class table will hold the class id, the subject, and the teacher assigned to it.
- The grades table will have the student id, the class id and the grade earned.
- 3.
- school=# SELECT * FROM student JOIN grades ON student.id = grades.student_id JOIN class ON grades.class_id = class.id WHERE class.id = 1;
- name | id | student_id | class_id | grade | id | subject
- ---------------------------+----+------------+----------+-------+----+----------
- Waylon Dalton | 1 | 1 | 1 | A | 1 | Language
- Justine Henderson | 2 | 2 | 1 | F | 1 | Language
- Marcus Cruz | 4 | 4 | 1 | B | 1 | Language
- Thalia Cobb | 5 | 5 | 1 | C | 1 | Language
- (4 rows)
- school=# SELECT grade FROM grades WHERE class_id = 1 ORDER BY grade;
- grade
- -------
- A
- B
- C
- F
- (4 rows)
- school=# SELECT class.subject, COUNT(grades.student_id) FROM grades JOIN class ON grades.class_id = class.id GROUP BY class.subject;
- subject | count
- ------------------+-------
- Computer Science | 4
- History | 4
- Science | 3
- Math | 5
- Language | 4
- (5 rows)
- school=# SELECT class.subject, COUNT(grades.student_id) FROM grades JOIN class ON grades.class_id = class.id GROUP BY class.subject ORDER BY count DESC LIMIT 1;
- subject | count
- ---------+-------
- Math | 5
- (1 row)
Add Comment
Please, Sign In to add comment