You are on page 1of 22

N01580206 Yug Sutariya

Lab Exercise #4
Grade: 1%
Objective: Select from one table.
Submission:
1. Save this Lab as Lab1_StudentNumber. For example:
Lab4_N1234567.
2. Perform all the following steps. 
3. Take a Screen Capture of each step and insert under each step.
4. Upload Pdf version of your word document in Lab Exercise
submission #4

Section A
How to retrieve data from two or more tables
1. Write a SELECT statement that joins the Courses table to the Departments table and returns these
columns: CourseNumber, CourseDescription, DepartmentName.
Sort the result set by DepartmentName and then by CourseNumber in ascending order.
Ans :-
SELECT c.CourseID,c.CourseDescription , d.DepartmentName
FROM Courses c JOIN Departments d
ON c.DepartmentID=d.DepartmentID
ORDER BY d.DepartmentName,CourseNumber;
N01580206 Yug Sutariya

2. Write a SELECT statement that joins the Instructors table to the Courses table and returns these
columns: LastName, FirstName, CourseNumber, CourseDescription.
Return all courses for each instructor with a status of “P” (part time).
Sort the result set by LastName and then by FirstName in ascending order.
Ans :-
SELECT i.LastName,i.FirstName,c.CourseNumber,c.CourseDescription
FROM Instructors i JOIN Courses c
ON i.DepartmentID=c.DepartmentID
WHERE i.Status='P'
ORDER BY LastName,FirstName;
N01580206 Yug Sutariya

3. Write a SELECT statement that joins the Departments, Courses, and Instructors tables. This
statement should return these columns: DepartmentName, CourseDescription, FirstName, and
LastName.
Use aliases for the tables, and return only those courses with three units.
Sort the result set by DepartmentName and then by CourseDescription in ascending sequence.
Ans :-
SELECT d.DepartmentName,c.CourseDescription,i.LastName,i.FirstName
FROM Departments d JOIN Instructors i ON d.DepartmentID=i.DepartmentID JOIN Courses c
ON i.DepartmentID=c.DepartmentID
WHERE c.CourseUnits=3
ORDER BY d.DepartmentName,c.CourseDescription;
N01580206 Yug Sutariya

4. Write a SELECT statement that joins the Departments, Courses, StudentCourses, and Students
tables. This statement should return these columns: DepartmentName, CourseDescription,
LastName, and FirstName.
Return all courses in the English department.
Sort the result set by DepartmentName and then by CourseDescription in ascending sequence.
Ans :-
SELECT d.DepartmentName,c.CourseDescription,i.LastName,i.FirstName
FROM Departments d JOIN Courses c ON d.DepartmentID=c.DepartmentID
JOIN Instructors i ON c.DepartmentID=i.DepartmentID
WHERE d.DepartmentName='English'
ORDER BY d.DepartmentName,c.CourseDescription;
N01580206 Yug Sutariya

5. Write a SELECT statement that joins the Instructors and Courses tables and returns these columns:
LastName, FirstName, and CourseDescription.
Return at least one row for each instructor, even if that instructor isn’t teaching any courses.
Sort the result set by LastName and then by FirstName. 
Ans :-
SELECT i.LastName,i.FirstName,c.CourseDescription
FROM Instructors i LEFT JOIN Courses c
ON i.DepartmentID=c.DepartmentID
ORDER BY i.LastName,i.FirstName;
N01580206 Yug Sutariya

6. Use the UNION operator to generate a result set consisting of five columns from the Students table: 
Status A calculated column that contains a value of UNDERGRAD or GRADUATED
FirstName The FirstName column
LastName The LastName column
EnrollmentDate The EnrollmentDate column
N01580206 Yug Sutariya

GraduationDate The GraduationDate column


If the student doesn’t have a value in the GraduationDate column, the Status column should contain a value
of UNDERGRAD. Otherwise, it should contain a value of GRADUATED.
Sort the final result set by EnrollmentDate. 
Ans :-
SELECT 'UNDERGRAD'AS Status, FirstName,LastName,EnrollmentDate,GraduationDate
FROM Students
WHERE GraduationDate IS NULL
UNION
SELECT 'GRADUATED'AS Status, FirstName,LastName,EnrollmentDate,GraduationDate
FROM Students
WHERE GraduationDate IS NOT NULL
ORDER BY EnrollmentDate;

7. Write a SELECT statement that returns these two columns: 


DepartmentName The DepartmentName column from the Departments table
CourseID The CourseID column from the Courses table
Return one row for each Department with no courses. (Hint: Use an outer join and only return rows where
the CourseID column contains a null value.)
Ans:-
SELECT d.DepartmentName,c.CourseID
from Departments d LEFT JOIN Courses c
ON d.DepartmentID=c.DepartmentID
WHERE c.CourseID IS NULL
N01580206 Yug Sutariya

8. Write a SELECT statement that returns these columns:


InstructorDept The DepartmentName column from the Departments table for a related instructor
LastName The LastName column from the Instructors table
FirstName The FirstName column from the Instructors table
CourseDescription The CourseDescription column from the Courses table
CourseDept The DepartmentName column from the Departments table for a related instructor
Return one row for each course that’s in a different department than the department of the instructor
assigned to teach that course. (Hint: You will need to join the Departments table to both the Instructors
table and the Courses table, which will require you to use table aliases to distinguish the two tables.)
Ans:-
SELECT d.DepartmentName as
"InstructorDept",i.LastName,i.FirstName,c.CourseDescription,d.DepartmentName as
"CourseDept"
FROM Instructors i JOIN Departments d ON i.DepartmentID=d.DepartmentID RIGHT JOIN Courses c
ON d.DepartmentID=c.DepartmentID;
N01580206 Yug Sutariya

Section B
How to code summary queries
1. Write a SELECT statement that returns these columns:
The count of the number of instructors in the Instructors table
The average of the AnnualSalary column in the Instructors table
Include only those rows where the Status column is equal to “F” (Fulltime).
Ans :-
SELECT COUNT(InstructorID) AS 'Number Of Instructors',AVG(AnnualSalary) AS 'Average
Salary'
FROM Instructors
WHERE Status='F';
N01580206 Yug Sutariya

2. Write a SELECT statement that returns one row for each department that has instructors with
these columns:
The DepartmentName column from the Departments table
The count of the instructors in the Instructors table
The annual salary of the highest paid instructor in the Instructors table
Sort the result set so the department with the most instructors appears first.
Ans :-
SELECT d.DepartmentName,COUNT(*) as 'Number Of Instructors' , MAX(i.AnnualSalary) AS
'Highest Salary'
FROM Departments d JOIN Instructors i ON
d.DepartmentID=i.DepartmentID
GROUP BY d.DepartmentName
ORDER BY 'Number of Instructors' DESC;
N01580206 Yug Sutariya

hn

1. Write a SELECT statement that returns one row for each instructor that has courses with these
columns:
The instructor first and last names from the Instructors table in this format: John Doe (Note: If the
instructor first name has a null value, the concatenation of the first and last name will result in a
null value.)
A count of the number of courses in the Courses table
The sum of the course units in the Courses table
(Hint: You will need to concatenate the instructor first and last names again in the GROUP BY clause.)
Sort the result set in descending sequence by the total course units for each instructor.
Ans :-
SELECT i.FirstName+' '+i.LastName AS 'Name' , COUNT(*) AS 'Number Of Courses' ,
SUM(CourseUnits) AS 'Total'
FROM Instructors i JOIN Courses c
ON i.DepartmentID=c.DepartmentID
GROUP BY I.FirstName+' '+I.LastName
ORDER BY 'Total' DESC;
N01580206 Yug Sutariya

2. Write a SELECT statement that returns one row for each course that has students enrolled with
these columns:
The DepartmentName column from the Departments table
The CourseDescription from the Courses table
A count of the number of students from the StudentCourses table
Sort the result set by DepartmentName, then by the enrollment for each course.
Ans :-
SELECT d.DepartmentName,c.CourseDescription,COUNT(sc.StudentID) AS 'Number of Students'
FROM Departments d JOIN Courses c ON d.DepartmentID=c.DepartmentID
JOIN StudentCourses sc ON c.CourseID=sc.CourseID
JOIN Students st ON sc.StudentID=st.StudentID
GROUP BY d.DepartmentName,c.CourseDescription,c.CourseNumber
ORDER BY d.DepartmentName,c.CourseNumber;
N01580206 Yug Sutariya

3. Write a SELECT statement that returns one row for each student that has courses with these
columns:
The StudentID column from the Students table
The sum of the course units in the Courses table
Sort the result set in descending sequence by the total course units for each student.
Ans :-
SELECT s.StudentID,SUM(c.CourseUnits) AS 'Total'
FROM Students s JOIN StudentCourses sc
ON s.StudentID=sc.StudentID JOIN Courses c
ON sc.CourseID=c.CourseID
GROUP BY s.StudentID
ORDER BY 'Total' DESC;
N01580206 Yug Sutariya

4. Write a SELECT statement that answers this question: What is the total number of courses
taught by parttime instructors? Return these columns:
The instructor last name and first name from the Instructors table in this format: Doe, John (Note: If the
instructor first name has a null value, the concatenation of the first and last name will result in a
null value.)
The total number of courses taught for each instructor in the Courses table
Use the ROLLUP operator to include a row that gives the grand total.
Ans :-
SELECT i.FirstName+' '+i.LastName AS 'Name' , COUNT(c.CourseID) AS 'Number Of Courses'
FROM Instructors i JOIN Courses c
ON i.DepartmentID=c.DepartmentID
WHERE i.Status='P'
GROUP BY ROLLUP(I.FirstName+' '+I.LastName);
N01580206 Yug Sutariya

Section C
How to code subqueries
1. Write a SELECT statement that returns the same result set as this SELECT statement, but don’t use a
join. Instead, use a subquery in a WHERE clause that uses the IN keyword.
SELECT DISTINCT LastName, FirstName
FROM Instructors i JOIN Courses c
  ON i.InstructorID = c.InstructorID
ORDER BY LastName, FirstName
Ans :-
SELECT DISTINCT LastName, FirstName
FROM Instructors
WHERE InstructorID IN (SELECT InstructorID FROM Courses)
ORDER BY LastName,FirstName;
N01580206 Yug Sutariya

2. Write a SELECT statement that answers this question: Which instructors have an annual salary
that’s greater than the average annual salary for all instructors?
Return the LastName, FirstName, and AnnualSalary columns for each Instructor.
Sort the result set by the AnnualSalary column in descending sequence.
Ans :-
SELECT LastName,FirstName,AnnualSalary
FROM Instructors
WHERE AnnualSalary>(SELECT AVG(AnnualSalary) FROM Instructors)
ORDER BY AnnualSalary DESC;
N01580206 Yug Sutariya

3. Write a SELECT statement that returns the LastName and FirstName columns from the
Instructors table.
Return one row for each instructor that doesn’t have any courses in the Courses table. To do that, use a
subquery introduced with the NOT EXISTS operator.
Sort the result set by LastName and then by FirstName.
Ans :-
SELECT LastName,FirstName
FROM Instructors
WHERE NOT EXISTS
(SELECT * FROM Courses WHERE Courses.InstructorID=Instructors.InstructorID)
ORDER BY LastName,FirstName;
N01580206 Yug Sutariya

4. Write a SELECT statement that returns the LastName and FirstName columns from the Students
table, along with a count of the number of courses each student is taking from the
StudentCourses table.
Return one row for each student who is taking more than one class. To do that, use a subquery with the IN
class that groups the student course by StudentID.
Group and sort the result set by the LastName and then by the FirstName.
Ans :-
SELECT s.LastName,s.Firstname,COUNT(sc.CourseID) AS 'Number Of Courses'
FROM Students s JOIN StudentCourses SC ON s.StudentID=sc.StudentID
WHERE s.StudentID IN (SELECT StudentID FROM StudentCourses sc JOIN Courses c
ON sc.CourseID=c.CourseID
GROUP BY StudentID
HAVING COUNT(sc.CourseID)>1)
GROUP BY s.LastName,s.FirstName
ORDER BY s.LastName,s.FirstName;
N01580206 Yug Sutariya

5 Write a SELECT statement that returns the LastName, FirstName, and AnnualSalary columns of
each instructor that has a unique annual salary. In other words, don’t include instructors that
have the same annual salary as another instructor.
Sort the results by LastName and then by FirstName.
Ans :-
SELECT LastName,FirstName,AnnualSalary
FROM Instructors
WHERE AnnualSalary NOT IN (SELECT AnnualSalary from Instructors
GROUP BY AnnualSalary
HAVING COUNT(AnnualSalary)>1)
N01580206 Yug Sutariya

6 Write a SELECT statement that returns one row for each course with these columns:
The CourseID column from the Courses table
The most recent enrollment date for that course from the Students table
Change the SELECT statement to a CTE. Then, write a SELECT statement that returns one row per course that
shows the CourseDescription for the course and the LastName, FirstName, and EnrollmentDate for the
student with the most recent enrollment data.
Ans :-
WITH tab as
(
SELECT c.CourseID,MAX(s.EnrollmentDate) AS 'Latest Enrollment Date'
FROM Courses c JOIN StudentCourses sc
ON c.CourseID=sc.CourseID
JOIN Students s ON sc.StudentID=s.StudentID
GROUP BY c.CourseID
)
SELECT c.CourseDescription,s.LastName,s.FirstName,s.EnrollmentDate
FROM Courses c JOIN StudentCourses sc
ON c.CourseID=sc.CourseID
JOIN Students s ON sc.StudentID=s.StudentID
JOIN tab tb ON c.CourseID=tb.CourseID
WHERE s.EnrollmentDate=tb.[Latest Enrollment Date]
N01580206 Yug Sutariya

7 Write a SELECT statement that returns one row for each student that has courses with these
columns:
The StudentID column from the Students table
The sum of the course units in the Courses table
Include only those students who are taking more than 9 units (fulltime).
Change the SELECT statement to a CTE. Then, write a SELECT statement that uses this CTE to return the
student ID, sum of course units, and the tuition. (The tuition is equal to the FullTimeCost column, plus
the PerUnitCost column multiplied by the number of units.)
To do that, you can use a cross join to add the columns from the Tuition table to the query. This works
because there’s only one row in the Tuition table.
Ans :-

WITH cte1 as
(
SELECT s.StudentID,SUM(c.CourseUnits) AS 'Total Courses'
FROM Courses c JOIN StudentCourses sc
ON c.CourseID=sc.CourseID JOIN
Students s ON sc.StudentID=s.StudentID
GROUP BY s.StudentID
HAVING SUM(c.CourseUnits)>9
)
SELECT s.StudentID,FullTimeCost+(PerUnitCost*s.[Total Courses]) AS 'Tuition'
FROM cte1 s CROSS JOIN Tuition
N01580206 Yug Sutariya

You might also like