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

SQL Practice Questions

The document contains SQL practice questions and answers related to creating, inserting, deleting, and retrieving data from various tables including Students, Courses, Enrollments, Professors, and Departments. It covers SQL commands for table creation, data manipulation, and querying with conditions, aggregate functions, grouping, and joins. The document serves as a comprehensive guide for practicing SQL operations in a relational database context.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

SQL Practice Questions

The document contains SQL practice questions and answers related to creating, inserting, deleting, and retrieving data from various tables including Students, Courses, Enrollments, Professors, and Departments. It covers SQL commands for table creation, data manipulation, and querying with conditions, aggregate functions, grouping, and joins. The document serves as a comprehensive guide for practicing SQL operations in a relational database context.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

SQL PRACTICE QUESTIONS

Tables

1. Students

• StudentID (int, Primary Key)


• FirstName (varchar(255))
• LastName (varchar(255))
• EnrollmentDate (date)
• Major (varchar(255))
• Email (varchar(255), Optional)

2. Courses

• CourseID (int, Primary Key)


• CourseName (varchar(255))
• Credits (int or decimal(4,2))

3. Enrollments

• EnrollmentID (int, Primary Key)


• StudentID (int, Foreign Key references Students(StudentID))
• CourseID (int, Foreign Key references Courses(CourseID))
• Grade (char(1))

4. Professors

• ProfessorID (int, Primary Key)


• FirstName (varchar(255))
• LastName (varchar(255))
• Department (varchar(255))

5. Departments

• DepartmentID (int, Primary Key)


• DepartmentName (varchar(255))

-- Create Students Table

CREATE TABLE Students (

StudentID INT PRIMARY KEY,

FirstName VARCHAR(255),
LastName VARCHAR(255),

EnrollmentDate DATE,

Major VARCHAR(255),

Email VARCHAR(255)

);

-- Create Courses Table

CREATE TABLE Courses (

CourseID INT PRIMARY KEY,

CourseName VARCHAR(255),

Credits INT

);

-- Create Enrollments Table

CREATE TABLE Enrollments (

EnrollmentID INT PRIMARY KEY,

StudentID INT,

CourseID INT,

Grade CHAR(1),

FOREIGN KEY (StudentID) REFERENCES Students(StudentID),

FOREIGN KEY (CourseID) REFERENCES Courses(CourseID)

);

-- Create Professors Table

CREATE TABLE Professors (


ProfessorID INT PRIMARY KEY,

FirstName VARCHAR(255),

LastName VARCHAR(255),

Department VARCHAR(255)

);

-- Create Departments Table

CREATE TABLE Departments (

DepartmentID INT PRIMARY KEY,

DepartmentName VARCHAR(255)

);

-- Create Teaches Table

CREATE TABLE Teaches (

ProfessorID INT,

CourseID INT,

FOREIGN KEY (ProfessorID) REFERENCES Professors(ProfessorID),

FOREIGN KEY (CourseID) REFERENCES Courses(CourseID)

);

-- Insert Data into Students Table

INSERT INTO Students (StudentID, FirstName, LastName, EnrollmentDate, Major, Email) VALUES

(1, 'Alice', 'Smith', '2023-09-01', 'Computer Science', '[email protected]'),

(2, 'Bob', 'Johnson', '2023-09-01', 'Mathematics', '[email protected]'),


(3, 'Carol', 'Williams', '2023-09-01', 'Physics', '[email protected]'),

(4, 'Dave', 'Brown', '2023-09-01', 'Chemistry', NULL);

-- Insert Data into Courses Table

INSERT INTO Courses (CourseID, CourseName, Credits) VALUES

(101, 'Introduction to Programming', 3),

(102, 'Introduction to Databases', 3);

-- Insert Data into Enrollments Table

INSERT INTO Enrollments (EnrollmentID, StudentID, CourseID, Grade) VALUES

(1001, 1, 101, 'A'),

(1002, 2, 102, 'B');

-- Insert Data into Professors Table

INSERT INTO Professors (ProfessorID, FirstName, LastName, Department) VALUES

(1, 'John', 'Doe', 'Computer Science'),

(2, 'Emma', 'Davis', 'Biology');

-- Insert Data into Departments Table

INSERT INTO Departments (DepartmentID, DepartmentName) VALUES

(1, 'Computer Science'),

(2, 'Mathematics'),

(3, 'Physics'),

(4, 'Chemistry'),

(5, 'Biology');
-- Insert Data into Teaches Table

INSERT INTO Teaches (ProfessorID, CourseID) VALUES

(1, 101),

(2, 102);

1. Creation (DDL)

Q1: How do you create a table named Students with columns StudentID, FirstName,
LastName, EnrollmentDate, and Major?

CREATE TABLE Students (


StudentID int PRIMARY KEY,
FirstName varchar(255),
LastName varchar(255),
EnrollmentDate date,
Major varchar(255)
);

Q2: How do you create a table named Courses with columns CourseID, CourseName,
and Credits?

CREATE TABLE Courses (


CourseID int PRIMARY KEY,
CourseName varchar(255),
Credits int
);

Q3: How do you create a table named Enrollments with columns EnrollmentID,
StudentID, CourseID, and Grade?

CREATE TABLE Enrollments (


EnrollmentID int PRIMARY KEY,
StudentID int,
CourseID int,
Grade char(1),
FOREIGN KEY (StudentID) REFERENCES Students(StudentID),
FOREIGN KEY (CourseID) REFERENCES Courses(CourseID)
);

Q4: How do you create a table named Professors with columns ProfessorID,
FirstName, LastName, and Department?

CREATE TABLE Professors (


ProfessorID int PRIMARY KEY,
FirstName varchar(255),
LastName varchar(255),
Department varchar(255)
);

Q5: How do you create a table named Departments with columns DepartmentID and
DepartmentName?

CREATE TABLE Departments (


DepartmentID int PRIMARY KEY,
DepartmentName varchar(255)
);

Q6: How do you add a new column Email to the Students table?

ALTER TABLE Students


ADD COLUMN Email varchar(255);

Q7: How do you modify the Credits column in the Courses table to be decimal(4,2)?

ALTER TABLE Courses


MODIFY COLUMN Credits decimal(4, 2);

Q8: How do you remove the Grade column from the Enrollments table?

ALTER TABLE Enrollments


DROP COLUMN Grade;

Q9: How do you rename the Professors table to Faculty?

ALTER TABLE Professors


RENAME TO Faculty;

Q10: How do you create an index on the LastName column of the Students table?

CREATE INDEX idx_lastname ON Students(LastName);

2. Insertion (DML)

Q1: How do you insert a new student into the Students table?

INSERT INTO Students (StudentID, FirstName, LastName, EnrollmentDate,


Major)
VALUES (1, 'Alice', 'Smith', '2023-09-01', 'Computer Science');

Q2: How do you insert a new course into the Courses table?

INSERT INTO Courses (CourseID, CourseName, Credits)


VALUES (101, 'Introduction to Programming', 3);

Q3: How do you insert a new enrollment into the Enrollments table?

INSERT INTO Enrollments (EnrollmentID, StudentID, CourseID, Grade)


VALUES (1001, 1, 101, 'A');

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?

INSERT INTO Departments (DepartmentID, DepartmentName)


VALUES (1, 'Computer Science');

Q6: How do you insert multiple students into the Students table?

INSERT INTO Students (StudentID, FirstName, LastName, EnrollmentDate,


Major)
VALUES (2, 'Bob', 'Johnson', '2023-09-01', 'Mathematics'),
(3, 'Carol', 'Williams', '2023-09-01', 'Physics');

Q7: How do you insert a new course into the Courses table with a course name
containing an apostrophe?

INSERT INTO Courses (CourseID, CourseName, Credits)


VALUES (102, 'Introduction to Database\'s', 3);

Q8: How do you insert a new student with a NULL value for Email in the Students
table?

INSERT INTO Students (StudentID, FirstName, LastName, EnrollmentDate,


Major, Email)
VALUES (4, 'Dave', 'Brown', '2023-09-01', 'Chemistry', NULL);

Q9: How do you insert a new professor into the Professors table with a department
that does not yet exist in the Departments table?

INSERT INTO Professors (ProfessorID, FirstName, LastName, Department)


VALUES (2, 'Emma', 'Davis', 'Biology');

Q10: How do you insert a new department only if it does not already exist in the
Departments table?

INSERT INTO Departments (DepartmentID, DepartmentName)


SELECT 2, 'Mathematics'
WHERE NOT EXISTS (SELECT 1 FROM Departments WHERE DepartmentID = 2);

3. Deletion (DML)

Q1: How do you delete a student with StudentID 1 from the Students table?

DELETE FROM Students


WHERE StudentID = 1;

Q2: How do you delete a course with CourseID 101 from the Courses table?

DELETE FROM Courses


WHERE CourseID = 101;
Q3: How do you delete all enrollments for a student with StudentID 2?

DELETE FROM Enrollments


WHERE StudentID = 2;

Q4: How do you delete a professor with ProfessorID 1 from the Professors table?

DELETE FROM Professors


WHERE ProfessorID = 1;

Q5: How do you delete a department with DepartmentID 1 from the Departments table?

DELETE FROM Departments


WHERE DepartmentID = 1;

Q6: How do you delete all students who enrolled before the year 2023?

DELETE FROM Students


WHERE EnrollmentDate < '2023-01-01';

Q7: How do you delete all courses with 3 credits from the Courses table?

DELETE FROM Courses


WHERE Credits = 3;

Q8: How do you delete all rows from the Enrollments table?

DELETE FROM Enrollments;

Q9: How do you delete a professor if the department is not 'Computer Science'?

DELETE FROM Professors


WHERE Department != 'Computer Science';

Q10: How do you delete a student with NULL email from the Students table?

DELETE FROM Students


WHERE Email IS NULL;

4. Dropping (DDL)

Q1: How do you drop the Students table?

DROP TABLE Students;

Q2: How do you drop the Courses table?

DROP TABLE Courses;

Q3: How do you drop the Enrollments table?

DROP TABLE Enrollments;


Q4: How do you drop the Professors table?

DROP TABLE Professors;

Q5: How do you drop the Departments table?

DROP TABLE Departments;

Q6: How do you drop the index idx_lastname on the Students table?

DROP INDEX idx_lastname;

Q7: How do you drop a column Email from the Students table?

ALTER TABLE Students


DROP COLUMN Email;

Q8: How do you drop the Faculty table?

DROP TABLE Faculty;

Q9: How do you drop a foreign key constraint in the Enrollments table on StudentID?

ALTER TABLE Enrollments


DROP FOREIGN KEY fk_student;

Q10: How do you drop a primary key constraint from the Courses table?

ALTER TABLE Courses


DROP PRIMARY KEY;

5. Retrieving Data with Conditions (WHERE)

Q1: How do you retrieve all students who are majoring in 'Computer Science'?

SELECT * FROM Students


WHERE Major = 'Computer Science';

Q2: How do you retrieve all courses with more than 3 credits?

SELECT * FROM Courses


WHERE Credits > 3;

Q3: How do you retrieve all enrollments for a student with StudentID 1?

SELECT * FROM Enrollments


WHERE StudentID = 1;

Q4: How do you retrieve all professors in the 'Mathematics' department?

SELECT * FROM Professors


WHERE Department = 'Mathematics';
Q5: How do you retrieve all departments that have 'Science' in their name?

SELECT * FROM Departments


WHERE DepartmentName LIKE '%Science%';

Q6: How do you retrieve all students who enrolled after '2023-01-01'?

SELECT * FROM Students


WHERE EnrollmentDate > '2023-01-01';

Q7: How do you retrieve all courses with exactly 3 credits?

SELECT * FROM Courses


WHERE Credits = 3;

Q8: How do you retrieve all enrollments where the grade is 'A'?

SELECT * FROM Enrollments


WHERE Grade = 'A';

Q9: How do you retrieve all professors with a last name starting with 'D'?

SELECT * FROM Professors


WHERE LastName LIKE 'D%';

Q10: How do you retrieve all students with a NULL email?

SELECT * FROM Students


WHERE Email IS NULL;

6. Aggregate Functions

Q1: How do you find the total number of students?

SELECT COUNT(*) as TotalStudents FROM Students;

Q2: How do you find the average credits for all courses?

SELECT AVG(Credits) as AverageCredits FROM Courses;

Q3: How do you find the highest grade received in the Enrollments table?

SELECT MAX(Grade) as HighestGrade FROM Enrollments;

Q4: How do you find the lowest grade received in the Enrollments table?

SELECT MIN(Grade) as LowestGrade FROM Enrollments;

Q5: How do you find the sum of credits for all courses?

SELECT SUM(Credits) as TotalCredits FROM Courses;

Q6: How do you find the count of students in each major?


SELECT Major, COUNT(*) as NumberOfStudents FROM Students
GROUP BY Major;

Q7: How do you find the average grade for a student with StudentID 1?

SELECT AVG(Grade) as AverageGrade FROM Enrollments


WHERE StudentID = 1;

Q8: How do you find the number of professors in each department?

SELECT Department, COUNT(*) as NumberOfProfessors FROM Professors


GROUP BY Department;

Q9: How do you find the maximum credits offered by any course?

SELECT MAX(Credits) as MaxCredits FROM Courses;

Q10: How do you find the minimum credits offered by any course?

SELECT MIN(Credits) as MinCredits FROM Courses;

7. Grouping (GROUP BY)

Q1: How do you group students by major and count the number of students in each
major?

SELECT Major, COUNT(*) as NumberOfStudents FROM Students


GROUP BY Major;

Q2: How do you group courses by credits and count the number of courses with each
credit value?

SELECT Credits, COUNT(*) as NumberOfCourses FROM Courses


GROUP BY Credits;

Q3: How do you group enrollments by course and calculate the average grade for each
course?

SELECT CourseID, AVG(Grade) as AverageGrade FROM Enrollments


GROUP BY CourseID;

Q4: How do you group professors by department and count the number of professors in
each department?

SELECT Department, COUNT(*) as NumberOfProfessors FROM Professors


GROUP BY Department;

Q5: How do you group students by enrollment year and count the number of students
enrolled each year?

SELECT YEAR(EnrollmentDate) as EnrollmentYear, COUNT(*) as NumberOfStudents


FROM Students
GROUP BY YEAR(EnrollmentDate);
Q6: How do you group courses by the first letter of the course name and count the
number of courses for each letter?

SELECT LEFT(CourseName, 1) as FirstLetter, COUNT(*) as NumberOfCourses FROM


Courses
GROUP BY LEFT(CourseName, 1);

Q7: How do you group enrollments by student and count the number of courses each
student is enrolled in?

SELECT StudentID, COUNT(*) as NumberOfCourses FROM Enrollments


GROUP BY StudentID;

Q8: How do you group professors by the first letter of their last name and count the
number of professors for each letter?

SELECT LEFT(LastName, 1) as FirstLetter, COUNT(*) as NumberOfProfessors


FROM Professors
GROUP BY LEFT(LastName, 1);

Q9: How do you group students by major and calculate the average enrollment year for
each major?

SELECT Major, AVG(YEAR(EnrollmentDate)) as AverageEnrollmentYear FROM


Students
GROUP BY Major;

Q10: How do you group enrollments by course and count the number of students
enrolled in each course?

SELECT CourseID, COUNT(*) as NumberOfStudents FROM Enrollments


GROUP BY CourseID;

8. Joins

Q1: How do you retrieve all students along with their enrollments?

SELECT Students.*, Enrollments.* FROM Students


JOIN Enrollments ON Students.StudentID = Enrollments.StudentID;

Q2: How do you retrieve all enrollments along with the corresponding course names?

SELECT Enrollments.*, Courses.CourseName FROM Enrollments


JOIN Courses ON Enrollments.CourseID = Courses.CourseID;

Q3: How do you retrieve all courses along with the professors teaching them, assuming
there's a Teaches table?

SELECT Courses.*, Professors.FirstName, Professors.LastName FROM Courses


JOIN Teaches ON Courses.CourseID = Teaches.CourseID
JOIN Professors ON Teaches.ProfessorID = Professors.ProfessorID;
Q4: How do you retrieve all students along with their majors and the corresponding
department names?

SELECT Students.*, Departments.DepartmentName FROM Students


JOIN Departments ON Students.Major = Departments.DepartmentName;

Q5: How do you retrieve all professors along with the courses they teach, assuming
there's a Teaches table?

SELECT Professors.*, Courses.CourseName FROM Professors


JOIN Teaches ON Professors.ProfessorID = Teaches.ProfessorID
JOIN Courses ON Teaches.CourseID = Courses.CourseID;

Q6: How do you retrieve all students along with their grades in each course?

SELECT Students.*, Courses.CourseName, Enrollments.Grade FROM Students


JOIN Enrollments ON Students.StudentID = Enrollments.StudentID
JOIN Courses ON Enrollments.CourseID = Courses.CourseID;

Q7: How do you retrieve all departments along with the number of professors in each
department?

SELECT Departments.*, COUNT(Professors.ProfessorID) as NumberOfProfessors


FROM Departments
LEFT JOIN Professors ON Departments.DepartmentName = Professors.Department
GROUP BY Departments.DepartmentName;

Q8: How do you retrieve all courses along with the number of students enrolled in each
course?

SELECT Courses.*, COUNT(Enrollments.StudentID) as NumberOfStudents FROM


Courses
LEFT JOIN Enrollments ON Courses.CourseID = Enrollments.CourseID
GROUP BY Courses.CourseID;

Q9: How do you retrieve all professors who do not teach any course, assuming there's a
Teaches table?

SELECT Professors.* FROM Professors


LEFT JOIN Teaches ON Professors.ProfessorID = Teaches.ProfessorID
WHERE Teaches.CourseID IS NULL;

Q10: How do you retrieve all students who are not enrolled in any course?

SELECT Students.* FROM Students


LEFT JOIN Enrollments ON Students.StudentID = Enrollments.StudentID
WHERE Enrollments.CourseID IS NULL;

9. HAVING

Q1: How do you retrieve the number of students in each major, but only for majors
with more than 10 students?

SELECT Major, COUNT(*) as NumberOfStudents FROM Students


GROUP BY Major
HAVING COUNT(*) > 10;

Q2: How do you retrieve the number of courses in each credit category, but only for
categories with more than 5 courses?

SELECT Credits, COUNT(*) as NumberOfCourses FROM Courses


GROUP BY Credits
HAVING COUNT(*) > 5;

Q3: How do you retrieve the average grade for each course, but only for courses with an
average grade of 'B' or higher?

SELECT CourseID, AVG(Grade) as AverageGrade FROM Enrollments


GROUP BY CourseID
HAVING AVG(Grade) >= 'B';

Q4: How do you retrieve the number of professors in each department, but only for
departments with more than 3 professors?

SELECT Department, COUNT(*) as NumberOfProfessors FROM Professors


GROUP BY Department
HAVING COUNT(*) > 3;

Q5: How do you retrieve the number of students enrolled each year, but only for years
with more than 100 students?

SELECT YEAR(EnrollmentDate) as EnrollmentYear, COUNT(*) as NumberOfStudents


FROM Students
GROUP BY YEAR(EnrollmentDate)
HAVING COUNT(*) > 100;

Q6: How do you retrieve the number of courses starting with each letter, but only for
letters with more than 3 courses?

SELECT LEFT(CourseName, 1) as FirstLetter, COUNT(*) as NumberOfCourses FROM


Courses
GROUP BY LEFT(CourseName, 1)
HAVING COUNT(*) > 3;

Q7: How do you retrieve the number of students each professor is teaching, but only for
professors teaching more than 5 students?

SELECT Professors.ProfessorID, COUNT(Enrollments.StudentID) as


NumberOfStudents FROM Professors
JOIN Teaches ON Professors.ProfessorID = Teaches.ProfessorID
JOIN Enrollments ON Teaches.CourseID = Enrollments.CourseID
GROUP BY Professors.ProfessorID
HAVING COUNT(Enrollments.StudentID) > 5;

Q8: How do you retrieve the average credits for each department, but only for
departments with an average of more than 3 credits?

SELECT Department, AVG(Credits) as AverageCredits FROM Courses


GROUP BY Department
HAVING AVG(Credits) > 3;

Q9: How do you retrieve the number of enrollments for each course, but only for
courses with more than 20 enrollments?

SELECT CourseID, COUNT(*) as NumberOfEnrollments FROM Enrollments


GROUP BY CourseID
HAVING COUNT(*) > 20;

Q10: How do you retrieve the number of students with each grade, but only for grades
assigned to more than 10 students?

SELECT Grade, COUNT(*) as NumberOfStudents FROM Enrollments


GROUP BY Grade
HAVING COUNT(*) > 10;

MADE WITH

BY Ashish Kumar Singh

You might also like