0% found this document useful (0 votes)
0 views9 pages

Commonly Asked DBMS Interview Questions - Set 2

This document provides a set of commonly asked interview questions related to Database Management Systems (DBMS), including SQL queries for finding duplicate rows, second highest salary, and above-average students. It also explains concepts like primary vs unique keys, materialized vs dynamic views, and embedded vs dynamic SQL. Additionally, it includes practical examples and SQL syntax for various scenarios, making it a useful resource for interview preparation.
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)
0 views9 pages

Commonly Asked DBMS Interview Questions - Set 2

This document provides a set of commonly asked interview questions related to Database Management Systems (DBMS), including SQL queries for finding duplicate rows, second highest salary, and above-average students. It also explains concepts like primary vs unique keys, materialized vs dynamic views, and embedded vs dynamic SQL. Additionally, it includes practical examples and SQL syntax for various scenarios, making it a useful resource for interview preparation.
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
You are on page 1/ 9

Commonly asked DBMS Interview Questions | Set 2

Last Updated : 25 Oct, 2024

This article is an extension of Commonly asked DBMS interview questions | Set 1.

Q1. There is a table where only one row is fully repeated. Write a Query to find the
Repeated row

Name Section

abc CS1

bcd CS2

abc CS1

In the above table, we can find duplicate rows using the below query.

SELECT name, section FROM tbl


GROUP BY name, section
HAVING COUNT(*) > 1

Q2. 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);

Q3. Consider the following Employee table. How many rows are there in the result of the
following query?

ID Salary DeptName

1 10000 EC

2 40000 EC
ID Salary DeptName

3 30000 CS

4 40000 ME

5 50000 ME

6 60000 ME

7 70000 CS

How many rows are there in the result of the 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 the result of the query as 3000 is the minimum salary of CS Employees
and all these rows are greater than 30000. 2 4 5 6 7

Q4. 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 follows:

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;

Q5. There is a table which contains two columns 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;

Q6. Name the Employee who has the third-highest salary 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 a salary higher than this person will be 2.

Q7. 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.

Note: It is not a predefined rule but by and large you’ll see that in a good number of the SQL
queries,
Winter Tickets we
Sale!useAptitude
WHERE prior toMathematics
Engineering GROUP BYDiscrete
and HAVING afterOperating
Mathematics GROUP BY. The
System Where
DBMS clause
Computer acts Digital
Networks
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 40

a c2 50

b c3 60

d c1 70

e c2 80
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,
the output of the below query

SELECT Student, sum(score) AS total FROM Marks

Student Total

a 90

b 60

d 70

e 80

When we apply to have in above query, we get

SELECT Student, sum(score) AS total


FROM Marks having total > 70

Student Total

a 90

e 80

Q8. Difference between primary key and unique key and why one should use a 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 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.

Q9. 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.

Q10. 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.

Static (embedded) SQL Dynamic (interactive) SQL

In static SQL how database will be accessed is In dynamic SQL, how database will be accessed
predetermined in the embedded SQL statement. is determined at run time.

It is more swift and efficient. It is less swift and efficient.

SQL statements are compiled at compile time. SQL statements are compiled at run time.

Parsing, validation, optimization, and generation of Parsing, validation, optimization, and generation
application plan are done at compile time. of application plan are done at run time.

It is generally used for situations where data is It is generally used for situations where data is
distributed uniformly. distributed non-uniformly.

EXECUTE IMMEDIATE, EXECUTE and PREPARE EXECUTE IMMEDIATE, EXECUTE and PREPARE
statements are not used. statements are used.

It is less flexible. It is more flexible.

Q11. What is the difference between CHAR and VARCHAR?

CHAR and VARCHAR differ in storage and retrieval.


CHAR column length is fixed while VARCHAR length is variable.
The maximum no. of characters CHAR data type can hold is 255 characters while VARCHAR can
hold up to 4000 characters.
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

Are you a student in Computer Science or an employed professional looking to take up the GATE
2025 Exam? Of course, you can get a good score in it but to get the best score our GATE CS/IT 2025
- Self-Paced Course is available on GeeksforGeeks to help you with its preparation. Get
comprehensive coverage of all topics of GATE, detailed explanations, and practice questions for
study. Study at your pace. Flexible and easy-to-follow modules. Do well in GATE to enhance the
prospects of your career. Enroll now and let your journey to success begin!

GeeksforGeeks 61

Previous Article Next Article


Commonly asked DBMS interview questions Database Management System - GATE CSE Previous
Year Questions

Similar Reads
Commonly asked DBMS interview questions
1. What are the advantages of DBMS over traditional file-based systems? Database management
systems were developed to handle the following difficulties of typical File-processing systems…
15+ min read

Commonly Asked C++ Interview Questions | Set 2


Q. Major Differences between Java and C++ There are lot of differences, some of the major differences
are: Java has automatic garbage collection whereas C++ has destructors, which are automatically…
8 min read

Commonly asked JavaScript Interview Questions | Set 1


What is JavaScript(JS)? JavaScript is a lightweight, interpreted programming language with object-
oriented capabilities that allows you to build interactivity into otherwise static HTML pages.What are…
4 min read

Commonly Asked C++ Interview Questions | Set 1


Refer to the article C++ Interview Questions and Answers for the latest data. 1. What are the
differences between C and C++? C++ is a kind of superset of C, most C programs except for a few…
5 min read

Commonly Asked Java Programming Interview Questions | Set 2


In this article, some of the most important Java Interview Questions and Answers are discussed, to give
you the cutting edge in your interviews. Java is one of the most popular and widely used programming…
10 min read

Commonly asked Computer Networks Interview Questions | Set 1


What are Unicasting, Anycasting, Multicasting and Broadcasting? If the message is sent from a source
to a single destination node, it is called Unicasting. This is typically done in networks. If the message is…
4 min read

Commonly Asked Java Programming Interview Questions | Set 1


Why is Java called the ‘Platform Independent Programming Language’? Platform independence means
that execution of your program does not dependent on type of operating system(it could be any : Linux…
5 min read

Commonly Asked C Programming Interview Questions | Set 2


This post is second set of Commonly Asked C Programming Interview Questions | Set 1What are main
characteristics of C language? C is a procedural language. The main features of C language include lo…
3 min read

Commonly Asked C Programming Interview Questions | Set 1


What is the difference between declaration and definition of a variable/function Ans: Declaration of a
variable/function simply declares that the variable/function exists somewhere in the program but the…
5 min read

Commonly Asked Algorithm Interview Questions


For tech job interviews, knowing about algorithms is really important. Being good at algorithms shows
you can solve problems effectively and make programs run faster. This article has lots of common…
15+ min read

Article Tags : DBMS Interview Questions interview-questions

Corporate & Communications Address:-


A-143, 9th Floor, Sovereign Corporate
Tower, Sector- 136, Noida, Uttar Pradesh
(201305) | Registered Address:- K 061,
Tower K, Gulshan Vivante Apartment,
Sector 137, Noida, Gautam Buddh
Nagar, Uttar Pradesh, 201305

Company Explore
About Us Job-A-Thon Hiring Challenge
Legal Hack-A-Thon
Careers GfG Weekly Contest
In Media Offline Classes (Delhi/NCR)
Contact Us DSA in JAVA/C++
Advertise with us Master System Design
GFG Corporate Solution Master CP
Placement Training Program GeeksforGeeks Videos
Geeks Community

Languages DSA
Python Data Structures
Java Algorithms
C++ DSA for Beginners
PHP Basic DSA Problems
GoLang DSA Roadmap
SQL DSA Interview Questions
R Language Competitive Programming
Android Tutorial

Data Science & ML Web Technologies


Data Science With Python HTML
Data Science For Beginner CSS
Machine Learning JavaScript
ML Maths TypeScript
Data Visualisation ReactJS
Pandas NextJS
NumPy NodeJs
NLP Bootstrap
Deep Learning Tailwind CSS

Python Tutorial Computer Science


Python Programming Examples GATE CS Notes
Django Tutorial Operating Systems
Python Projects Computer Network
Python Tkinter Database Management System
Web Scraping Software Engineering
OpenCV Tutorial Digital Logic Design
Python Interview Question Engineering Maths

DevOps System Design


Git High Level Design
AWS Low Level Design
Docker UML Diagrams
Kubernetes Interview Guide
Azure Design Patterns
GCP OOAD
DevOps Roadmap System Design Bootcamp
Interview Questions

School Subjects Commerce


Mathematics Accountancy
Physics Business Studies
Chemistry Economics
Biology Management
Social Science HR Management
English Grammar Finance
Income Tax
Databases Preparation Corner
SQL Company-Wise Recruitment Process
MYSQL Resume Templates
PostgreSQL Aptitude Preparation
PL/SQL Puzzles
MongoDB Company-Wise Preparation
Companies
Colleges

Competitive Exams More Tutorials


JEE Advanced Software Development
UGC NET Software Testing
UPSC Product Management
SSC CGL Project Management
SBI PO Linux
SBI Clerk Excel
IBPS PO All Cheat Sheets
IBPS Clerk Recent Articles

Free Online Tools Write & Earn


Typing Test Write an Article
Image Editor Improve an Article
Code Formatters Pick Topics to Write
Code Converters Share your Experiences
Currency Converter Internships
Random Number Generator
Random Password Generator

DSA/Placements Development/Testing
DSA - Self Paced Course JavaScript Full Course
DSA in JavaScript - Self Paced Course React JS Course
DSA in Python - Self Paced React Native Course
C Programming Course Online - Learn C with Data Structures Django Web Development Course
Complete Interview Preparation Complete Bootstrap Course
Master Competitive Programming Full Stack Development - [LIVE]
Core CS Subject for Interview Preparation JAVA Backend Development - [LIVE]
Mastering System Design: LLD to HLD Complete Software Testing Course [LIVE]
Tech Interview 101 - From DSA to System Design [LIVE] Android Mastery with Kotlin [LIVE]
DSA to Development [HYBRID]
Placement Preparation Crash Course [LIVE]

Machine Learning/Data Science Programming Languages


Complete Machine Learning & Data Science Program - [LIVE] C Programming with Data Structures
Data Analytics Training using Excel, SQL, Python & PowerBI - [LIVE] C++ Programming Course
Data Science Training Program - [LIVE] Java Programming Course
Mastering Generative AI and ChatGPT Python Full Course

Clouds/Devops GATE
DevOps Engineering GATE CS & IT Test Series - 2025
AWS Solutions Architect Certification GATE DA Test Series 2025
Salesforce Certified Administrator Course GATE CS & IT Course - 2025
GATE DA Course 2025

@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved

You might also like