SQL Coding Test
In this coding test, we will be providing you with code to construct an SQL schema. Some database operations will have to be performed on the schema and answers to those will be provided in the end.
Schema Setup Code
Run the below code in SQL:
CREATE SCHEMA SCHOOL;
CREATE TABLE Teachers (
T_id INT PRIMARY KEY,
T_name TEXT NOT NULL,
T_experience INT NOT NULL
CREATE TABLE DegreeProgram (
P_id INT PRIMARY KEY,
P_name TEXT NOT NULL,
T_id INT NOT NULL,
FOREIGN KEY f_key(T_id) REFERENCES Teachers(T_id)
CREATE TABLE Students (
S_id INT PRIMARY KEY,
S_name TEXT NOT NULL,
S_age INT NOT NULL,
S_address CHAR (50),
S_regsitration_no INT UNIQUE,
P_id INT NOT NULL
INSERT INTO Teachers(
(1, 'DR. EMILIA ROSE', 10),
(2, 'DR. CLARK KANE', 5),
(3, 'DR. GARRY BLACKWOOD', 7),
(4, 'DR. H.K. ROSE', 13),
(6, 'DR. JONATHAN LAKE', 4);
INSERT INTO DegreeProgram(
(1, 'BACHELORS OF ENGINEERING IN ELECTRICAL ENGINEERING', 6),
(2, 'BACHELORS OF ENGINEERING IN SOFTWARE ENGINEERING', 2),
(3, 'BACHELORS OF SCIENCES IN COMPUTER SCIENCE', 1),
(4, 'BACHELORS OF ENGINEERING IN MeCHANICAL ENGINEERING', 3),
(5, 'BACHELORS OF ENGINEERING IN CIVIL ENGINEERING', 4);
INSERT INTO students(
(0,'JOHN CONNER',19,'23 BOULEVARD ST.',1,3),
(1,'ADAM SMITH',21,'28 BOULEVARD ST.',2,1),
(2,'BLAKE JONES',23,'22 BOULEVARD ST.',3,5),
(3,'BRIDGETTE JOHNSON',18,'21 BOULEVARD ST.',4,5),
(4,'CMAILIA BLACK',20,'27 BOULEVARD ST.',5,3),
(5,'AMY WINEHOUSE',20,'23 BOULEVARD ST.',6,2),
(6,'CHRIS JONES',23,'01 BOULEVARD ST.',7,4),
(8,'JOHNATHAN CONNER',20,'MAIN BOULEVARD',10,1),
(7,'JOHN SMITH',21,'XYS ST. GATE',9,4);
Now that you have the tables and data you need let’s get to the questions:
Add a foreign key constraint on table students. Make attribute P_id refer to P_id in DegreePrograms table.
Add student with the following information in students table. S_id = 9 S_name = CAPRICE WINSELET S_age = 20 S_registration_no = 8 P_id = 1 S_address = ABC street 22-A
Try dropping the table teachers. See if it is successfully dropped or not. Explain the reason if it’s not successfully dropped.
Get the number of students in each degree program and label the column as ‘number of students’ in ascending order.
Show which teacher has the maximum experience.
See which program is taught by the teacher with minimum experience and who is the teacher. Show his details too.
Show all teachers who do not teach programs with id 1 and id 4.
Show how many degree programs are students enrolled in. Show their details.
See which student is taught by which teacher.
Delete all students who are greater than 22 years old.
To add a foreign key to an existing table, we use the ALTER TABLE command. The query to be used is:
ALTER TABLE students
ADD FOREIGN KEY f_key(P_id) REFERENCES degreeprogram(P_id);
To add a student, we will use the insert into statement supported by the SQL database.
INSERT INTO students
VALUES (9,'CAPRICE WINSELET',20,'ABC street 22-A',8,1);
The drop table statement is used to drop the table:
DROP TABLE teachers;
The table fails to drop because of the foreign key constraint on DegreePrograms table. So we find out that we can’t delete a parent table which is referred somewhere in another table.
Group by statement and join will be used to do what is asked in the question.
SELECT P_name, COUNT (*) AS 'Number of students'
FROM degreeprogram JOIN students
GROUP BY (P_id);
This answer to this question will use a nested query since we need the value from one query into another query.
SELECT T_name, T_experience
WHERE T_experience = (
This query will do the required operation:
SELECT T_name, T_experience, T_id, P_name
FROM teachers JOIN degreeprogram
WHERE T_experience = (
In this query, we will be using the OR operator in SQL along with the NOT IN operator.
WHERE T_id NOT IN (
WHERE P_id =1 OR P_id = 4
The DISTINCT clause that we studied in SQL is useful for this operation since the degree programs are repeated, we only need one instance of them.
WHERE P_id IN (
This task can be done with 3 nested queries or 3 tables join. we will do it using 3 table joins.
SELECT S_name, T_name
FROM (students JOIN degreeprogram USING (P_id)) JOIN teachers USING(T_ID);
This operation requires the use of the SQL DELETE clause.
DELETE FROM students
WHERE S_age > 22;
Thanks for taking our SQL coding test. We hope you were able to level up your PostgreSQL skills!
Pilot the ObjectRocket Platform Free!
Try Fully-Managed CockroachDB, Elasticsearch, MongoDB, PostgreSQL (Beta) or Redis.Get Started