Database Project Week
• Course (CourseID, Level, Sessions, Instructor, startDate, LessonTime) • Lessons (LessonID, CourseID, MemberID) • Members (MemberID, Firstname, Surname, DOB, Address, City)
Adding data with Mockaroo
EXERCISES: A. Use the SQL AND, OR and NOT Operators in your query (The WHERE clause can be combined with AND, OR, and NOT operators)
- Where courseID is equals to a number below 5 and the instructor of any of the instructors
SELECT * FROM Course WHERE CourseID < 5;
- Where courseID is equals to a number above 5 and the lesson time is in the morning or afternoon.
SELECT * FROM Course WHERE CourseID > 5 AND LessonTime > '6:00:00' AND LessonTime < '18:00:00'
B. Order by the above results by:
- startDate in “course” table
SELECT * FROM Course ORDER BY startDate ASC;
- MemberID in “members” table
SELECT * FROM members ORDER BY MemberID ASC;
C. UPDATE the following:
- Members table, change the addresses of any three members.
UPDATE Members SET Address='10 Downing Street' WHERE firstName='Davy';
UPDATE Members SET Address='10 Downing Street' WHERE firstName='Ivonne';
UPDATE Members SET Address='10 Downing Street' WHERE firstName='Nick';
- Course table, change the startDate and lesson time for three of the sessions.
UPDATE Course SET startDate='2022-01-01', LessonTime='12:00:00' WHERE CourseID=1;
UPDATE Course SET startDate='2022-01-02', LessonTime='13:00:00' WHERE CourseID=2;
UPDATE Course SET startDate='2022-01-03, LessonTime='14:00:00' WHERE CourseID=3;
D. Use the SQL MIN () and MAX () Functions to return the smallest and largest value
-
Of the LessonID column in the “lesson” table
SELECT MIN(LessonID) AS 'Min LessonID' FROM Lesson;
SELECT MAX(LessonID) AS 'Max LessonID' FROM Lesson;
-
Of the membersID column in the “members” table
SELECT MIN(MemberID) AS 'Min MemberID' FROM Members;
SELECT MAX(MemberID) AS 'Max MemberID' FROM Members;
E. Use the SQL COUNT (), AVG () and SUM () Functions for these:
- Count the total number of members in the “members” table
SELECT COUNT(MemberID) AS 'No of Members' FROM members;
- Count the total number of sessions in the” members” table
SELECT COUNT(Sessions) AS 'No of Sessions' FROM members;
- Find the average session time for all “sessions” in course table
SELECT AVG(LessonTime) AS 'Average Session time' FROM course;
F. WILDCARD queries (like operator)
a) Find all the people from the “members” table whose last name starts with A.
SELECT * FROM members WHERE Surname LIKE 'A%';
b) Find all the people from the “members” table whose last name ends with A.
SELECT * FROM members WHERE Surname LIKE '%A';
c) Find all the people from the “members” table that have “ab” in any position in the last name.
SELECT * FROM members WHERE Surname LIKE '%ab%';
d) Find all the people from the “members” table that that have “b” in the second position in their first name.
SELECT * FROM members WHERE Firstname LIKE '_b%';
e) Find all the people from the “members” table whose last name starts with “a” and are at least 3 characters in length:
SELECT * FROM members WHERE Surname LIKE 'a__%'
f) Find all the people from the “members” table whose last name starts with “a” and ends with “y”
SELECT * FROM members WHERE Surname LIKE 'a%y'
g) Find all the people from the “members” table whose last name does not starts with “a” and ends with “y”
SELECT * FROM members WHERE Surname NOT LIKE 'a%' AND Surname LIKE '%y'
G. What do you understand by LEFT and RIGHT join? Explain with an example.
Left join is when you have all the values of the left column, and the matching values of the right column. If no matches are found, NULL is returned from the right column.
Right join is when you have all the values of the right column, and the matching values of the left column. If no matches are found, NULL is returned from the left column.
Coutry Id Name 1 UK 2 USA 3 Germany
City cityId cityName countryId 1 London 1 2 Manchester 1 3 New York 2