0% found this document useful (0 votes)
8 views2 pages

SQL Queries for Employee and Student Data

Ta

Uploaded by

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

SQL Queries for Employee and Student Data

Ta

Uploaded by

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

Q1. Display the 3rd highest salary without using LIMIT, TOP.

SELECT salary FROM Employee e1 WHERE 2 = ( SELECT COUNT(DISTINCT salary)


FROM Employee e2 WHERE [Link] > [Link] );

Q2. Display all employees whose salary is above the average salary of their department.
SELECT emp_id, emp_name, dept_id, salary FROM Employee e WHERE salary > (
SELECT AVG(salary) FROM Employee WHERE dept_id = e.dept_id );

Q3. Find employees whose emp_name occurs more than once and show how many times they appear.
SELECT emp_name, COUNT(*) AS occurrences FROM Employee GROUP BY emp_name
HAVING COUNT(*) > 1;

Q4. Display employees who were hired in the last 2 years from today.
SELECT emp_id, emp_name, hire_date FROM Employee WHERE hire_date >=
DATE_SUB(CURDATE(), INTERVAL 2 YEAR);

Q5. Display department-wise employees, but show 'No Department' where dept_id is NULL.
SELECT emp_id, emp_name, COALESCE(dept_id, 'No Department') AS Department
FROM Employee;

Q6. Display the maximum and minimum salary difference of the company.
SELECT MAX(salary) - MIN(salary) AS salary_difference FROM Employee;

Q7. List the employees who have the same salary as at least one other employee.
SELECT emp_id, emp_name, salary FROM Employee WHERE salary IN ( SELECT
salary FROM Employee GROUP BY salary HAVING COUNT(*) > 1 );

Q8. Display employees hired in February.


SELECT emp_id, emp_name, hire_date FROM Employee WHERE MONTH(hire_date) =
2;

Q9. Delete all employees whose salary is less than the overall company average.
DELETE FROM Employee WHERE salary < (SELECT AVG(salary) FROM Employee);

Q10. Identify all candidate keys for the Student table.


Candidate Keys: student_id. (name, course_id) may be candidate if unique.

Q11. Display students who scored the highest marks per course.
SELECT s1.course_id, [Link], [Link] FROM Student s1 WHERE marks = (
SELECT MAX(marks) FROM Student s2 WHERE s1.course_id = s2.course_id );

Q12. Display students whose marks are equal to the class average.
SELECT * FROM Student WHERE marks = (SELECT AVG(marks) FROM Student);

Q13. Show duplicate names but with different emails.


SELECT name, COUNT(DISTINCT email) FROM Student GROUP BY name HAVING
COUNT(DISTINCT email) > 1;

Q14. Can student_id be a primary key here?


Yes, because it is unique for each student.

Q15. Can (name, course_id) be a candidate key?


Yes, if one student cannot take the same course multiple times.

Q16. Find students with the same marks but different course_id.
SELECT [Link], [Link], s1.course_id, s2.course_id FROM Student s1 JOIN
Student s2 ON [Link] = [Link] AND s1.course_id <> s2.course_id;

Q17. What will happen if you run: SELECT name, AVG(marks) FROM Student;
It will throw an error because 'name' is not in GROUP BY.

Q18. Display the average marks per course.


SELECT course_id, AVG(marks) AS avg_marks FROM Student GROUP BY
course_id;

Q19. What will happen if you run: SELECT name, AVG(marks) FROM Student GROUP BY course_id;
Error – because 'name' is not grouped properly.

Q20. Identify the primary key of Course_Registration.


Reg_ID is the Primary Key.

Q21. Is a composite key required?


Yes, (Student_Ref, Course_Ref, Semester) can be composite if Reg_ID not
present.

Q22. Identify foreign keys and their references.


Student_Ref → Student(Student_ID), Course_Ref → Course(Course_ID).

Q23. Difference in output: SELECT NULL = NULL; SELECT NULL IS NULL;


NULL = NULL → NULL, NULL IS NULL → TRUE.

Q24. Will this query work: SELECT 5 + '2';


Yes, '2' will be cast to integer. Output = 7.

Q25. If you execute DELETE FROM Employee; ROLLBACK;


If autocommit ON → deletion permanent. If OFF → rollback restores data.

Common questions

Powered by AI

A primary key is a unique identifier for a table, typically a single column like a student ID. A composite key, however, uses multiple columns to define uniqueness when a single column does not suffice as a unique identifier. In a student-course-registration context, (Student_Ref, Course_Ref, Semester) can serve as a composite key if Reg_ID is not available . This ensures each record is unique across the combination of these three attributes, particularly in tables lacking a singular unique field.

To find employees hired in the last two years, use the query: SELECT emp_id, emp_name, hire_date FROM Employee WHERE hire_date >= DATE_SUB(CURDATE(), INTERVAL 2 YEAR). This query uses the DATE_SUB function to subtract two years from the current date, thereby determining a date range to compare against the employees' hire dates.

To find employees with duplicate names and the number of occurrences, use: SELECT emp_name, COUNT(*) AS occurrences FROM Employee GROUP BY emp_name HAVING COUNT(*) > 1 . This query not only identifies duplicate employee names but also counts and displays how many times each duplicate occurs, providing both identification and frequency information in the dataset.

Maintaining unique student-course relations requires careful consideration of candidate keys. If each student-course instance is unique, a composite key like (name, course_id) can be suitable, provided there's no case for a student enrolling multiple times in the same course . However, uniqueness may often require a dedicated identifier such as course registration IDs. A composite key is ideal when no singular unique identifier suffices or is available to ensure data integrity effectively.

The query SELECT name, AVG(marks) FROM Student GROUP BY course_id will error because 'name' is not aggregated or grouped; SQL requires each selected field to either be included in the GROUP BY clause or wrapped in an aggregate function. To correct it, include 'name' in the GROUP BY clause if suitable or wrap the non-grouped field in an appropriate function . This illustrates the necessity of adhering to SQL's grouping rules to prevent execution errors.

To handle NULL department values in a department-wise employee query, use COALESCE to substitute 'No Department' for NULLs: SELECT emp_id, emp_name, COALESCE(dept_id, 'No Department') AS Department FROM Employee . COALESCE returns the first non-NULL value among its arguments, allowing alternative text representation for NULL department IDs.

Executing a DELETE statement followed by a ROLLBACK will have different results depending on the autocommit setting. If autocommit is ON, each statement is committed automatically, making the deletion permanent. If autocommit is OFF, the DELETE is only tentative until a COMMIT is issued, allowing ROLLBACK to restore the deleted data . This highlights the importance of understanding transaction management and autocommit settings in SQL operations.

In SQL, comparisons involving NULL require special handling because NULL represents an unknown value. Using SELECT NULL = NULL will return NULL because two unknowns cannot be determined as equal by standard comparison. However, SELECT NULL IS NULL correctly returns TRUE because the IS operator is used specifically for NULL checks, recognizing both sides as NULL . This highlights the importance of using IS NULL for accurate NULL value comparisons.

To display the third highest salary without using LIMIT or TOP, you can use a correlated subquery that counts distinct salaries greater than the current salary in the outer query. The query is: SELECT salary FROM Employee e1 WHERE 2 = ( SELECT COUNT(DISTINCT salary) FROM Employee e2 WHERE e2.salary > e1.salary ). This illustrates the SQL concept of correlated subqueries, where the inner query depends on the outer query for its evaluation.

To identify employees with non-unique names but different email addresses, use: SELECT name, COUNT(DISTINCT email) FROM Student GROUP BY name HAVING COUNT(DISTINCT email) > 1 . This query groups the data by name and counts distinct email addresses, displaying names where more than one unique email exists, highlighting the non-unique name condition with different emails.

You might also like