Professional Documents
Culture Documents
column1_name data_type(size),
column2_name data_type(size),
...
PRIMARY KEY (column_name(s)) -- Optional: Define a unique identifier column(s)
);
Courses:
course_id (INT PRIMARY KEY): Unique identifier for each course.
course_name (VARCHAR(100)): Name of the course.
department (VARCHAR(50)): Department offering the course (optional).
teacher_id (INT): Foreign key referencing the Teachers table (optional).
Enrollments:
enrollment_id (INT PRIMARY KEY): Unique identifier for each enrollment.
student_id (INT): Foreign key referencing the Students table.
course_id (INT): Foreign key referencing the Courses table.
semester (VARCHAR(20)): Semester the student enrolled (e.g., Fall 2023).
grade (CHAR(2)): Final grade achieved by the student in the course (optional).
Teachers (Optional):
teacher_id (INT PRIMARY KEY): Unique identifier for each teacher.
first_name (VARCHAR(50)): Teacher's first name.
last_name (VARCHAR(50)): Teacher's last name.
subject_expertise (VARCHAR(100)): Subjects the teacher specializes in (optional).
email (VARCHAR(100)): Teacher's email address (optional).
Relationships:
A Student can enroll in many Courses (One-to-Many).
A Course can have many Students enrolled (One-to-Many).
A Student can have one Parent/Guardian (One-to-One).
A Course can be taught by one Teacher (One-to-Many, optional).
A Teacher can teach many Courses (One-to-Many, optional).
Q1. Write a query to retrieve the EmpFname and EmpLname in a single column as
“FullName”. The first name and the last name must be separated with space.
SELECT CONCAT(EmpFname, ' ', EmpLname) AS 'FullName' FROM EmployeeInfo;
Q2. Write a query find number of employees whose DOB is between 02/05/1970 to 31/12/1975
and are grouped according to gender
SELECT COUNT(*), Gender FROM EmployeeInfo WHERE DOB BETWEEN '02/05/1970 ' AND
'31/12/1975' GROUP BY Gender;
Q3. Write a query to fetch details of employees whose EmpLname ends with an alphabet ‘A’
and contains five alphabets.
SELECT * FROM EmployeeInfo WHERE EmpLname LIKE '____a';
Q4. Write a query to fetch details of all employees excluding the employees with first names,
“Sanjay” and “Sonia” from the EmployeeInfo table.
SELECT * FROM EmployeeInfo WHERE EmpFname NOT IN ('Sanjay','Sonia');
Q5. Write a query to fetch details of employees with the address as “DELHI(DEL)”.
SELECT * FROM EmployeeInfo WHERE Address LIKE 'DELHI(DEL)%';
Q6. Write a query to fetch all employees who also hold the managerial position.
SELECT E.EmpFname, E.EmpLname, P.EmpPosition
FROM EmployeeInfo E INNER JOIN EmployeePosition P ON
E.EmpID = P.EmpID AND P.EmpPosition IN ('Manager');
Q7. Write a query to fetch the department-wise count of employees sorted by department’s
count in ascending order.
SELECT Department, count(EmpID) AS EmpDeptCount
FROM EmployeeInfo GROUP BY Department
ORDER BY EmpDeptCount ASC;
Q8. Write a SQL query to retrieve employee details from EmployeeInfo table who have a date
of joining in the EmployeePosition table.
SELECT * FROM EmployeeInfo E
WHERE EXISTS
(SELECT * FROM EmployeePosition P WHERE E.EmpId = P.EmpId);
Q9. Write a query to retrieve two minimum and maximum salaries from the EmployeePosition
table.
To retrieve two minimum salaries, you can write a query as below:
SELECT DISTINCT Salary FROM EmployeePosition E1
WHERE 2 >= (SELECTCOUNT(DISTINCT Salary)FROM EmployeePosition E2
WHERE E1.Salary >= E2.Salary) ORDER BY E1.Salary DESC;
Q10. Write a query to find the Nth highest salary from the table without using TOP/limit
keyword.
SELECT Salary
FROM EmployeePosition E1
WHERE N-1 = (
SELECT COUNT( DISTINCT ( E2.Salary ) )
FROM EmployeePosition E2
WHERE E2.Salary > E1.Salary );
Q12. Write a query to retrieve the list of employees working in the same department.
Select DISTINCT E.EmpID, E.EmpFname, E.Department
FROM EmployeeInfo E, Employee E1
WHERE E.Department = E1.Department AND E.EmpID != E1.EmpID;
Q13. Write a query to retrieve the last 3 records from the EmployeeInfo table.
SELECT * FROM EmployeeInfo WHERE
EmpID <=3 UNION SELECT * FROM
(SELECT * FROM EmployeeInfo E ORDER BY E.EmpID DESC)
AS E1 WHERE E1.EmpID <=3;
Q14. Write a query to find the third-highest salary from the EmpPosition table.
SELECT TOP 1 salary
FROM(
SELECT TOP 3 salary
FROM employee_table
ORDER BY salary DESC) AS emp
ORDER BY salary ASC;
Q15. Write a query to display the first and the last record from the EmployeeInfo table.
To display the first record from the EmployeeInfo table, you can write a query as follows:
SELECT
orders.order_id,
orders.customer_id,
goodreads.book_title,
orders.quantity
FROM goodreads
INNER JOIN orders
ON goodreads.book_id = orders.book_id -- Columns with same data type (integer)
WHERE goodreads.price >= 20;
The INNER JOIN returns only the rows where there is a matching book ID in both the
goodreads and orders tables, focusing on books with prices $20 and above.
Q18. LEFT JOIN
With LEFT JOIN, all rows from the left table (orders) are fetched, along with matching rows from the
right table (deliveries). If there is no matching data in the right table, the result will still include the
left table's data, with NULL values in the columns from the right table.
SELECT
orders.order_id,
deliveries.delivery_id,
deliveries.delivery_date,
deliveries.delivery_status
FROM orders
LEFT JOIN deliveries
ON orders.order_id = deliveries.order_id;
FULL OUTER JOIN
Now, let's explore the FULL OUTER JOIN which allows you to bring unmatched rows into the
results.
Ever wondered what happens when both tables have something to offer, but they're not a perfect
match?
A FULL OUTER JOIN returns all rows when there is a match in either the left or the right table. If
there is no match, NULL values are returned for columns from the table without a match.
SELECT
orders.order_id,
deliveries.delivery_id,
deliveries.delivery_date,
deliveries.delivery_status
FROM orders
FULL OUTER JOIN deliveries
ON orders.order_id = deliveries.order_id;
Running this query is like uniting two tables: orders and deliveries. Each row represents an attempt at
a match. If there's no match, you'll see NULL values in the columns from the table without a match.
You will use the syntax to create a simple stored procedure in SQL. But before that, create two
tables using the CREATE TABLE command that you will use throughout the article. You will
also insert some values in them using the INSERT INTO command.