0% found this document useful (0 votes)
10 views3 pages

SQL Test Paper for Database Developers

The document is an SQL test paper for database developers at BrightTech Solutions, containing instructions and a dataset description for managing employees, departments, projects, and salaries. It includes 18 questions that cover various SQL operations such as creating tables, updating records, deleting entries, and using aggregate functions, triggers, stored procedures, and views. Each question is assigned specific marks, indicating its complexity and importance.

Uploaded by

varshacpatilfct
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views3 pages

SQL Test Paper for Database Developers

The document is an SQL test paper for database developers at BrightTech Solutions, containing instructions and a dataset description for managing employees, departments, projects, and salaries. It includes 18 questions that cover various SQL operations such as creating tables, updating records, deleting entries, and using aggregate functions, triggers, stored procedures, and views. Each question is assigned specific marks, indicating its complexity and importance.

Uploaded by

varshacpatilfct
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

SQL Test Paper

Instructions:

Attempt all questions.

Use appropriate SQL syntax.

Marks are indicated against each question.

Dataset Description

You are working as a database developer for "BrightTech Solutions", which


maintains a relational database for managing employees, departments, projects, and
salaries. The following tables are used:

1. Employees

EmpID EmpName DeptID ManagerID JoinDate Salary Bonus Status


VARCHAR
INT VARCHAR INT INT DATE INT INT
(Active/Resigned)

2. Departments

DeptID DeptName Location


INT VARCHAR VARCHAR

3. Projects

ProjID ProjName DeptID StartDate EndDate

4. EmployeeProjects

EmpID ProjID HoursWorked


INT INT INT

Q1.
Create the Employees table with appropriate constraints including primary key, foreign
key (for DeptID), check (for Status), and unique constraint on EmpName.
(4 Marks)

Q2.
Write an SQL query to update the Status of employees to 'Resigned' who have not
worked on any project and joined before 2020.
(2 Marks)
Q3.
Write an SQL command to delete all employees whose salary is less than the average
salary of their department.
(2 Marks)

Q4.
List all employees whose bonus is greater than 10% of their salary using appropriate
arithmetic and logical operators.
(2 Marks)

Q5.
Insert a record into the Projects table with NULL EndDate and ensure it violates no
constraints.
(2 Marks)

Q6.
Explain the difference between DELETE, TRUNCATE, and DROP with examples.
(3 Marks)

Q7.
Write an SQL query to display each department’s name, number of employees, and
average salary using aggregate functions and GROUP BY.
(3 Marks)

Q8.
List employees who have worked more than 100 hours in total across all projects
using JOIN and GROUP BY.
(3 Marks)

Q9.
Find employees who worked on all projects assigned to their department using
subqueries or HAVING clause.
(3 Marks)

Q10.
Write a query to fetch the top 3 highest paid employees in each department using a
window function or correlated subquery.
(3 Marks)

Q11.
Display the name of employees who have not worked on any project using LEFT
JOIN.
(3 Marks)

Q12.
Create a Stored Procedure that accepts DeptID as input and returns the list of
employees in that department along with their total hours worked.
(5 Marks)
Q13.
Write a Trigger that prevents inserting an employee with a salary less than 3000.
(4 Marks)

Q14.
Create a CTE to calculate the cumulative bonus of employees ordered by JoinDate,
and display only those whose cumulative bonus exceeds 10000.
(4 Marks)

Q15.
Create a View ActiveEmployeesView that displays only active employees along with
their department name.
(3 Marks)

Q16.
Using a subquery, find the employee(s) whose salary is equal to the second highest
salary in the company.
(3 Marks)

Q17.
Create a Stored Procedure to assign a new project to all employees of a particular
department. The procedure should take DeptID and ProjID as input and insert
appropriate records into EmployeeProjects.
(5 Marks)

Q18.
Create a Trigger that updates the status of an employee to ‘Active’ when their salary
is increased by more than 20%.
(3 Marks)

Common questions

Powered by AI

To update the status of employees who have not worked on any projects and joined before 2020, an SQL query should utilize a subquery or a LEFT JOIN. This query can check for employees without matching entries in EmployeeProjects by using a LEFT JOIN on the EmployeeProjects table and filtering for NULL in ProjID. Additionally, include a condition that filters JoinDate to before 2020, thereby targeting the correct group of employees .

A stored procedure for assigning projects would accept DeptID and ProjID as inputs and employ a cursor or straightforward INSERT INTO ... SELECT query to add entries in the EmployeeProjects table. Integrating concepts of transaction control ensures all related operations are committed or rolled back together if an error occurs, ensuring data integrity. Conditional logic in the procedure ensures only active employees or those matching specific criteria are selected for assignment .

A CTE is significant in calculating cumulative bonuses as it allows for incremental calculations while improving query readability and structure. A CTE can be implemented using the WITH clause, calculating the running total of bonuses ordered by JoinDate through the use of window functions like ROW_NUMBER() and SUM() OVER(ORDER BY JoinDate). This approach efficiently handles cumulative aggregations and improves performance over complex subquery alternatives .

DELETE removes rows from a table based on a WHERE clause, allowing for conditional row deletion. It's transactional and can be rolled back. TRUNCATE removes all rows from a table, resets any auto-increment counters, and usually doesn't log individual row deletions, making it faster but non-transactional. DROP removes the table structure and data, making it irreversible. Use DELETE for removing specific records, TRUNCATE for a complete and fast cleanup while keeping table structure, and DROP for complete deletion of the table including its structure .

The Employees table can be structured with a primary key constraint on EmpID, ensuring each employee has a unique identifier. A foreign key constraint on DeptID links each employee to a valid department, which must exist in the Departments table. For the Status column, a check constraint should ensure the status is either 'Active' or 'Resigned', providing data integrity for employment status. Additionally, a unique constraint on EmpName ensures no two employees have the same name, which could be necessary for business logic depending on database requirements .

Identifying the top 3 highest-paid employees in each department involves using window functions. The ROW_NUMBER() function can be applied over a partitioned result set, with partitioning based on DeptID and ordering by Salary in descending order, to rank employees. A subquery or CTE can then filter for rows where rank is less than or equal to 3, extracting the top compensation packages by department .

A trigger that prevents the insertion of an employee with a salary less than 3000 needs to effectively manage rollback operations automatically if an insertion occurs without requiring explicit control flow manageable in an application. The challenge lies in ensuring that the trigger operates efficiently without significantly impacting database performance, particularly under high insert loads, and does not interfere with other processes if data correction or exceptions are needed for specific business scenarios .

To find employees who have not participated in any project, a LEFT JOIN between the Employees and EmployeeProjects tables can be performed, linking on EmpID. By selecting records with NULL entries in the ProjID from the joined table, only employees without any projects are listed, indicating those who have not worked on any projects .

Creating a View such as ActiveEmployeesView involves writing a SELECT query that joins Employees and Departments tables on DeptID, filtering for employees with a Status of 'Active'. Using a view simplifies query reuse, enhances security by abstracting complex joins and conditions, and presents a consistent interface that's easier for end-users or applications to query directly for reporting or operational needs .

A trigger set to execute after an UPDATE event on the Employees table can monitor salary changes. It checks if the new salary exceeds the old salary by more than 20% using OLD and NEW references in trigger logic. If true, it updates the Status to 'Active'. Implications include maintaining data consistency and automatically enforcing business rules but require careful management to prevent unintended multiple trigger firings during batch updates .

You might also like