SQL Practice Questions
SQL Practice Questions
Tables
1. Students
2. Courses
3. Enrollments
4. Professors
5. Departments
FirstName VARCHAR(255),
LastName VARCHAR(255),
EnrollmentDate DATE,
Major VARCHAR(255),
Email VARCHAR(255)
);
CourseName VARCHAR(255),
Credits INT
);
StudentID INT,
CourseID INT,
Grade CHAR(1),
);
FirstName VARCHAR(255),
LastName VARCHAR(255),
Department VARCHAR(255)
);
DepartmentName VARCHAR(255)
);
ProfessorID INT,
CourseID INT,
);
INSERT INTO Students (StudentID, FirstName, LastName, EnrollmentDate, Major, Email) VALUES
(2, 'Mathematics'),
(3, 'Physics'),
(4, 'Chemistry'),
(5, 'Biology');
-- Insert Data into Teaches Table
(1, 101),
(2, 102);
1. Creation (DDL)
Q1: How do you create a table named Students with columns StudentID, FirstName,
LastName, EnrollmentDate, and Major?
Q2: How do you create a table named Courses with columns CourseID, CourseName,
and Credits?
Q3: How do you create a table named Enrollments with columns EnrollmentID,
StudentID, CourseID, and Grade?
Q4: How do you create a table named Professors with columns ProfessorID,
FirstName, LastName, and Department?
Q5: How do you create a table named Departments with columns DepartmentID and
DepartmentName?
Q6: How do you add a new column Email to the Students table?
Q7: How do you modify the Credits column in the Courses table to be decimal(4,2)?
Q8: How do you remove the Grade column from the Enrollments table?
Q10: How do you create an index on the LastName column of the Students table?
2. Insertion (DML)
Q1: How do you insert a new student into the Students table?
Q2: How do you insert a new course into the Courses table?
Q3: How do you insert a new enrollment into the Enrollments table?
Q4: How do you insert a new professor into the Professors table?
INSERT INTO Professors (ProfessorID, FirstName, LastName, Department)
VALUES (1, 'John', 'Doe', 'Computer Science');
Q5: How do you insert a new department into the Departments table?
Q6: How do you insert multiple students into the Students table?
Q7: How do you insert a new course into the Courses table with a course name
containing an apostrophe?
Q8: How do you insert a new student with a NULL value for Email in the Students
table?
Q9: How do you insert a new professor into the Professors table with a department
that does not yet exist in the Departments table?
Q10: How do you insert a new department only if it does not already exist in the
Departments table?
3. Deletion (DML)
Q1: How do you delete a student with StudentID 1 from the Students table?
Q2: How do you delete a course with CourseID 101 from the Courses table?
Q4: How do you delete a professor with ProfessorID 1 from the Professors table?
Q5: How do you delete a department with DepartmentID 1 from the Departments table?
Q6: How do you delete all students who enrolled before the year 2023?
Q7: How do you delete all courses with 3 credits from the Courses table?
Q8: How do you delete all rows from the Enrollments table?
Q9: How do you delete a professor if the department is not 'Computer Science'?
Q10: How do you delete a student with NULL email from the Students table?
4. Dropping (DDL)
Q6: How do you drop the index idx_lastname on the Students table?
Q7: How do you drop a column Email from the Students table?
Q9: How do you drop a foreign key constraint in the Enrollments table on StudentID?
Q10: How do you drop a primary key constraint from the Courses table?
Q1: How do you retrieve all students who are majoring in 'Computer Science'?
Q2: How do you retrieve all courses with more than 3 credits?
Q3: How do you retrieve all enrollments for a student with StudentID 1?
Q6: How do you retrieve all students who enrolled after '2023-01-01'?
Q8: How do you retrieve all enrollments where the grade is 'A'?
Q9: How do you retrieve all professors with a last name starting with 'D'?
6. Aggregate Functions
Q2: How do you find the average credits for all courses?
Q3: How do you find the highest grade received in the Enrollments table?
Q4: How do you find the lowest grade received in the Enrollments table?
Q5: How do you find the sum of credits for all courses?
Q7: How do you find the average grade for a student with StudentID 1?
Q9: How do you find the maximum credits offered by any course?
Q10: How do you find the minimum credits offered by any course?
Q1: How do you group students by major and count the number of students in each
major?
Q2: How do you group courses by credits and count the number of courses with each
credit value?
Q3: How do you group enrollments by course and calculate the average grade for each
course?
Q4: How do you group professors by department and count the number of professors in
each department?
Q5: How do you group students by enrollment year and count the number of students
enrolled each year?
Q7: How do you group enrollments by student and count the number of courses each
student is enrolled in?
Q8: How do you group professors by the first letter of their last name and count the
number of professors for each letter?
Q9: How do you group students by major and calculate the average enrollment year for
each major?
Q10: How do you group enrollments by course and count the number of students
enrolled in each course?
8. Joins
Q1: How do you retrieve all students along with their enrollments?
Q2: How do you retrieve all enrollments along with the corresponding course names?
Q3: How do you retrieve all courses along with the professors teaching them, assuming
there's a Teaches table?
Q5: How do you retrieve all professors along with the courses they teach, assuming
there's a Teaches table?
Q6: How do you retrieve all students along with their grades in each course?
Q7: How do you retrieve all departments along with the number of professors in each
department?
Q8: How do you retrieve all courses along with the number of students enrolled in each
course?
Q9: How do you retrieve all professors who do not teach any course, assuming there's a
Teaches table?
Q10: How do you retrieve all students who are not enrolled in any course?
9. HAVING
Q1: How do you retrieve the number of students in each major, but only for majors
with more than 10 students?
Q2: How do you retrieve the number of courses in each credit category, but only for
categories with more than 5 courses?
Q3: How do you retrieve the average grade for each course, but only for courses with an
average grade of 'B' or higher?
Q4: How do you retrieve the number of professors in each department, but only for
departments with more than 3 professors?
Q5: How do you retrieve the number of students enrolled each year, but only for years
with more than 100 students?
Q6: How do you retrieve the number of courses starting with each letter, but only for
letters with more than 3 courses?
Q7: How do you retrieve the number of students each professor is teaching, but only for
professors teaching more than 5 students?
Q8: How do you retrieve the average credits for each department, but only for
departments with an average of more than 3 credits?
Q9: How do you retrieve the number of enrollments for each course, but only for
courses with more than 20 enrollments?
Q10: How do you retrieve the number of students with each grade, but only for grades
assigned to more than 10 students?
MADE WITH