You are on page 1of 3

Teaching and Learning Activity: Suggested Solutions

Module: Database Programming 2 (HDBP200-1)


Week number (Date): 7 (17 September 2020)
Unit covered: Unit 7

1 Suppose you have the following database table extracts

Users table
ID FIRST_NAME SURNAME PASSWORD EMAIL PHONE
200 Charles Muranda $2y$10$NQ7bvhXo0iI... charlesm@boston.co.za 0218154800
201 Agnes Aishal $2y$10$NQ7bvWcq4…. agnes@gmail.com 0125468953
202 Joe Doe $2y$10$NQ7bvwhXlktl8…. jdoe@yahoo.com 0254762384
203 Steven Kong $2y$10$NQ7bkvyhW…. skong@email.com 0549273845

Roles table
ID ROLE
0 Admin
1 Student

User Information Table


ID USER_ID DOB ROLE_ID GENDER_ID CITY
15 199 2015-02-29 1 0 Johannesburg
16 200 2011-01-30 0 1 Cape town
17 201 2005-03-31 1 1 Lusaka
18 205 2006-04-12 1 2 Luanda

1 HDBP200-1-Jul-Dec2020-T&L-Memo-CMu-W7-08092020
Question
Write an appropriate SQL command that would result in the following:
a)
ID FIRST_NAME SURNAME PASSWORD EMAIL PHONE
201 Agnes Aishal $2y$10$NQ7bvWcq4…. agnes@gmail.com 0125468953
202 Joe Doe $2y$10$NQ7bvwhXlktl8…. jdoe@yahoo.com 0254762384
203 Steven Kong $2y$10$NQ7bkvyhW…. skong@email.com 0549273845
200 Charles Muranda $2y$10$NQ7bvhXo0iI... charlesm@boston.co.za 0218154800

SELECT * FROM Users ORDER BY `SURNAME` DESC

b)
ID FIRST_NAME SURNAME EMAIL PHONE
200 Charles Muranda charlesm@boston.co.za 0218154800
201 Agnes Aishal agnes@gmail.com 0125468953
202 Joe Doe jdoe@yahoo.com 0254762384
203 Steven Kong skong@email.com 0549273845

SELECT `ID`, `FIRST_NAME`, `SURNAME`, `EMAIL`, `PHONE` FROM Users

c)
ID FIRST_NAME SURNAME PASSWORD EMAIL PHONE
201 Agnes Aishal $2y$10$NQ7bvWcq4…. agnes@gmail.com 0125468953
202 Joe Doe $2y$10$NQ7bvwhXlktl8…. jdoe@yahoo.com 0254762384
203 Steven Kong $2y$10$NQ7bkvyhW…. skong@email.com 0549273845

SELECT * FROM Users WHERE ID > 200

d)
FIRST_NAME SURNAME PHONE
Charles Muranda 0218154800
Agnes Aishal 0125468953
Joe Doe 0254762384

SELECT `FIRST_NAME`, `SURNAME`, `PHONE` FROM Users

2 HDBP200-1-Jul-Dec2020-T&L-Memo-CMu-W7-08092020
e)
FIRST_NAME SURNAME ROLE

Charles Muranda Admin

Agnes Aishal Student

SELECT `FIRST_NAME`, `SURNAME`, `ROLES.ROLE` AS `ROLE` FROM Users


INNER JOIN UserInformation ON Users.ID = UserInformation.USER_ID

3 HDBP200-1-Jul-Dec2020-T&L-Memo-CMu-W7-08092020

You might also like