0% found this document useful (0 votes)
116 views

10 Lab Practice 2 Q1 Q10

This document contains SQL queries and answers related to a Sales database. It asks the reader to write SQL statements to retrieve information from tables like Customer and Salesperson. The queries include selecting columns, filtering rows, using operators like BETWEEN and IN, and pattern matching with LIKE. The document is formatted as a lab exercise, with the main questions on page 1 and follow up additional questions on subsequent pages.

Uploaded by

raeds
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
116 views

10 Lab Practice 2 Q1 Q10

This document contains SQL queries and answers related to a Sales database. It asks the reader to write SQL statements to retrieve information from tables like Customer and Salesperson. The queries include selecting columns, filtering rows, using operators like BETWEEN and IN, and pattern matching with LIKE. The document is formatted as a lab exercise, with the main questions on page 1 and follow up additional questions on subsequent pages.

Uploaded by

raeds
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

CT042-3-1-IDB Lab Practice 2

Lab Practice 2 (Q1 – Q10)

Use the Sales database which you had created in the previous week for this exercise.

Write the SQL statements for the following queries:

1. List the names and IDs for all customers.

Answer:

SELECT CustName, CustID


FROM Customer;

1a) List the Names and IDs for all salespersons.

2. List all details for all customers.

Answer:

SELECT *
FROM Customer;

2a) List all details for all salespersons.

3. List the IDs and names of all salespersons. Rename these columns to ‘Salesperson _ID’
and ‘Salesperson_Name’ respectively.

Answer:

SELECT EmpID AS 'Salesperson _ID', EmpName AS 'Salesperson_Name'


FROM Salesperson;

3a) List the IDs, names and cities for all customers. Label these columns as
‘Customer Number’, ‘Name’ and ‘City’ respectively.

4. List the names of all cities which all customers live in.

Answer:

SELECT DISTINCT CustCity


FROM Customer;

4a) List the rank levels of all salespersons.

_________________________________________________________________________
Page 1 of 4
CT042-3-1-IDB Lab Practice 2
5. List the name, monthly and annual salaries for all salespersons. Label these columns as
EmpName, Monthly Salary and Annual Salary respectively.

Answer:

SELECT EmpName, Salary AS 'Monthly Salary', Salary*12 AS 'Annual Salary'


FROM Salesperson;

5a) List the name, monthly and quarterly salaries for all salespersons. Label these
columns as EmpName, Monthly Salary and Quarterly Salary.

6. List the IDs and names of all salespersons with a salary greater than 2000.

Answer:

SELECT EmpID, EmpName, Salary


FROM Salesperson
WHERE Salary > 2000.00;

6a) List the IDs and names of all customers from ‘Kuala Lumpur’.

6b) List the IDs and names of all employees with rank level 2.

7. List the names of all customers from ‘Kuala Lumpur’ or ‘Ipoh’.

Answer:

SELECT CustName, CustCity


FROM Customer
WHERE (CustCity = 'Kuala Lumpur') OR (CustCity ='Ipoh');

-- Use IN as an alternative.

SELECT CustName, CustCity


FROM Customer
WHERE CustCity IN ('Kuala Lumpur', 'Ipoh');

7a) List the IDs and names of all salespersons with rank levels ‘1’ and ‘2’.

_________________________________________________________________________
Page 2 of 4
CT042-3-1-IDB Lab Practice 2
8. List the names and rank levels of all salespersons with a salary between 1500 and 2000.

Answer:

SELECT EmpName, RankLevel, Salary


FROM Salesperson
WHERE (Salary >= 1500.00) AND (Salary <= 2000.00);

-- Use BETWEEN as an alternative.

SELECT EmpName, RankLevel, Salary


FROM Salesperson
WHERE Salary BETWEEN 1500.00 AND 2000.00;

8a) List the IDs and names of all salespersons with rank levels between ‘1’ and ‘3’.

9. List the names of customers with credit ratings other than ‘B’ and ‘C’.

Answer:

SELECT *
FROM Customer
WHERE CreditRating NOT IN ('B', 'C');

-- Alternative answer

SELECT *
FROM Customer
WHERE (CreditRating != 'B') AND (CreditRating < > 'C');

9a) List the IDs and names of all customers who are not from ‘Ipoh’ or ‘Georgetown’.

1. List the names of all customers whose names contain the letters ‘rk’.

Answer:

SELECT CustName
FROM Customer
WHERE CustName LIKE '%rk%';

_________________________________________________________________________
Page 3 of 4
CT042-3-1-IDB Lab Practice 2
10a) List the names and IDs of all salespersons whose names contain the string ‘ri’.

10b) List the names and IDs of all salespersons whose names start with the letter ‘B’

10c) List the names and IDs of all salespersons whose names end with the letter ‘s’.

_________________________________________________________________________
Page 4 of 4

You might also like