Commonly asked DBMS interview questions|Set 2

28 Jun, 2021
This article is extension of Commonly asked DBMS interview questions | Set 1.
Q. There is a table where only one row is fully repeated. Write a Query to find the Repeated row
In the above table, we can find duplicate row using below query.
SELECT name, section FROM tbl
GROUP BY name, section
HAVING COUNT(*) > 1

Q. Query to find 2nd highest salary of an employee?
SELECT max(salary) FROM EMPLOYEES WHERE salary IN
(SELECT salary FROM EMPLOYEEs MINUS SELECT max(salary)
FROM EMPLOYEES);
OR
SELECT max(salary) FROM EMPLOYEES WHERE 
salary <> (SELECT max(salary) FROM EMPLOYEES);
Q.Consider the following Employee table. How many rows are there in the result of following query?
ID   salary   DeptName1    10000      EC2    40000      EC3    30000      CS4    40000      ME5    50000      ME6    60000      ME7    70000      CS
How many rows are there in the result of following query?
SELECT E.ID
FROM  Employee E
WHERE  EXISTS  (SELECT E2.salary
FROM Employee E2
WHERE E2.DeptName = 'CS'
AND   E.salary > E2.salary)
Following 5 rows will be result of query as 3000 is the minimum salary of CS Employees and all these rows are greater than 30000.
24567
Q. Write a trigger to update Emp table such that, If an updation is done in Dep table then salary of all employees of that department should be incremented by some amount (updation)
Assuming Table name are Dept and Emp, trigger can be written as –
CREATE OR REPLACE TRIGGER update_trig
AFTER UPDATE ON Dept
FOR EACH ROW
DECLARE
CURSOR emp_cur IS SELECT * FROM Emp;
BEGIN
FOR i IN emp_cur LOOP
IF i.dept_no = :NEW.dept_no THEN
DBMS_OUTPUT.PUT_LINE(i.emp_no);  --  for printing those
UPDATE Emp                      -- emp number which are
SET sal = i.sal + 100           -- updated
WHERE emp_no = i.emp_no;
END IF;
END LOOP;
END;
Q. There is a table which contains two column Student and Marks, you need to find all the students, whose marks are greater than average marks i.e. list of above average students.
SELECT student, marks 
FROM table
WHERE marks > SELECT AVG(marks) from table;
Q.Name the student who has secured third highest marks using sub queries.
SELECT Emp1.Name
FROM Employee Emp1
WHERE 2 = (SELECT COUNT(DISTINCT(Emp2.Salary))
           FROM Employee Emp2
           WHERE Emp2.Salary > Emp1.Salary
          )
*LOGIC- Number of people with salary higher than this person will be 2.
Q. Why we cannot use WHERE clause with aggregate functions like HAVING ?
The difference between the having and where clause in SQL is that the where clause canNOT be used with aggregates, but the having clause can. Please note : It is not a predefined rule but by and large you’ll see that in a good number of the SQL queries, we use WHERE prior to GROUP BY and HAVING after GROUP BY.
The Where clause acts as a pre filter where as Having as a post filter.
The where clause works on row’s data, not on aggregated data.
Let us consider below table ‘Marks’.
Student       Course      Score
a                c1             40a                c2             50b                c3             60d                c1             70e                c2             80
Consider the query
SELECT Student, sum(Score) AS total 
FROM Marks
This would select data row by row basis. The having clause works on aggregated data.
For example,  output of below query
SELECT Student, sum(score) AS total FROM Marks
Student     Totala             90b             60d             70e             80
When we apply having in above query, we get
SELECT Student, sum(score) AS total
FROM Marks having total > 70
Student     Totala             90e 80
Q. Difference between primary key and unique key and why one should use unique key if it allows only one null ?
Primary key:
Only one in a row(tuple).
Never allows null value(only key field).
Unique key identifier and can not be null and must be unique.
Unique Key:
Can be more than one unique key in one row.
Unique key can have null values(only single null is allowed).
It can be a candidate key.
Unique key can be null and may not be unique.
Q. What’s the difference between materialized and dynamic view?
Materialized views
Disk based and are updated periodically based upon the query definition.
A materialized table is created or updated infrequently and it must be synchronized with its associated base tables.
Dynamic views
Virtual only and run the query definition each time they are accessed.
A dynamic view may be created every time that a specific view is requested by the user.
Q. What is embedded and dynamic SQL? 
Static or Embedded SQL
SQL statements in an application that do not change at runtime and, therefore, can be hard-coded into the application.
Dynamic SQL
SQL statements that are constructed at runtime; for example, the application may allow users to enter their own queries.
Dynamic SQL is a programming technique that enables you to buildSQL statements dynamically at runtime. You can create more general purpose, flexible applications by using dynamic SQL because the full text of a SQL statement may be unknown at compilation.
http://docs.oracle.com/cd/A87860_01/doc/appdev.817/a76939/adg09dyn.htm
Q. What is the difference between CHAR and VARCHAR?
CHAR and VARCHAR are differ in storage and retrieval.
CHAR column length is fixed while VARCHAR length is variable.
The maximum no. of character CHAR data type can hold is 255 character while VARCHAR can hold up to 4000 character.
CHAR is 50% faster than VARCHAR.
CHAR uses static memory allocation while VARCHAR uses dynamic memory allocation.
You may also like:
Practice Quizzes on DBMS
Last Minute Notes – DBMS
DBMS Articles
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
DBMS
DBMS
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
CTE in SQL
Difference between Clustered and Non-clustered index
Data Preprocessing in Data Mining
Difference between DDL and DML in DBMS
Difference between SQL and NoSQL
Difference between DELETE, DROP and TRUNCATE
Indexing in Databases | Set 1
Third Normal Form (3NF)
Deadlock in DBMS
Second Normal Form (2NF)