The Wayback Machine - https://web.archive.org/web/20241006110911/https://www.geeksforgeeks.org/commonly-asked-dbms-interview-questions-set-2/
Open In App

Commonly asked DBMS interview questions | Set 2

Last Updated : 16 Jun, 2024
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

This article is an 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

Name Section
abc CS1
bcd CS2
abc CS1

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 the following query? 

ID   salary   DeptName 1    10000      EC 2    40000      EC 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 

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 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;

Q. 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;

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

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. 

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             40 a                c2             50 b                c3             60 d                c1             70 e                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, 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 

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

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.
Static (embedded) SQL Dynamic (interactive) SQL
In static SQL how database will be accessed is predetermined in the embedded SQL statement. In dynamic SQL, how database will be accessed 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 application plan are done at compile time. Parsing, validation, optimization, and generation of application plan are done at run time.
It is generally used for situations where data is distributed uniformly. It is generally used for situations where data is distributed non-uniformly.
EXECUTE IMMEDIATE, EXECUTE and PREPARE statements are not used. EXECUTE IMMEDIATE, EXECUTE and PREPARE statements are used.
It is less flexible. It is more flexible.

Q. 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:



Previous Article
Next Article

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 supported by conventional operating systems. 1. Data redundancy and inconsistency 2. Difficulty in accessing data 3. Data isolation – multiple files and formats 4.
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 invoked when the object is destroyed.Java does not support pointers, templates, unions, operator overloading, structures etc.C++ has no in built support
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 the features of JavaScript?JavaScript is a lightweight, interpreted programming language. JavaScript is designed for creating network-centric applica
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 exceptions (See this and this) work in C++ as well. C is a procedural programming language, but C++ supports both procedural and Object Oriented programming. Si
5 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 memory is not allocated for them. But the declaration of a variable/function serves an important role. And that is the type of the variable/functi
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 language and platform. Java is fast, reliable and secure. From desktop to web applications, scientific supercomputers to gaming consoles, cell ph
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 sent from a source to any of the given destination nodes. This is used a lot in Content delivery Systems where we want to get content from a
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, windows, Mac ..etc). So compile code only once and run it on any System (In C/C++, we need to compile the code for every machine on which we run it). J
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 low-level access to memory, simple set of keywords, and clean style. These features make it suitable for system programming like operating system or compi
3 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 interview questions about algorithms. Table of Content Commonly Asked Interview Questions on Sorting AlgorithmCommonly Asked Interview Questions on Sea
15+ min read
Most Commonly Asked System Design Interview Problems/Questions
This System Design Interview Guide will provide the most commonly asked system design interview questions and equip you with the knowledge and techniques needed to design, build, and scale your robust applications, for professionals and newbies Below are a list of most commonly asked interview problems/case studies/questions in System Design interv
2 min read
Commonly Asked Data Structure Interview Questions
In the world of tech interviews, knowing about data structures is super important for candidate aiming for jobs in computer science. Being good at data structures shows you can solve problems well and make programs run faster. This article is packed with top interview questions and answers about data structures. It's here to help you get ready for
15+ min read
Top 10 Most Commonly Asked Web3 Interview Questions and Answers
Web3 (also known as Web 3.0) is definitely the latest technology that the world is currently basking on and slowly small and big companies are turning to Web3 to not only retain their customer but also offer safer and private cyberspace for them to use. It provides better data privacy by eliminating central organizations and allowing users to keep
7 min read
Commonly Asked Questions in Goldman Sachs Interviews
Reverse words in a given string Number Formation Overlapping rectangles Ugly Numbers Column name from a given column number Stock buy and sell Non Repeating Character Merge Two Sorted Arrays Egg Dropping Puzzle Squares in N*N Chessboard Rectangles in N*N Board Wave Array LRU Cache Get minimum element from stack Implement Queue using array Queue usi
1 min read
Commonly asked questions in Flipkart Interviews
Snake and Ladder Problem Chocolate Distribution Problem Solve the Sudoku Shortest direction 0 - 1 Knapsack Problem Inversion of array Maximum of all subarrays of size k Possible words from Phone digits Doubling the value Print the Kth Digit Consecutive 1's not allowed Search in a Rotated Array Kadane's Algorithm Rat in a Maze Problem Multiply two s
1 min read
Amazon’s most frequently asked interview questions | Set 2
Amazon's Most Frequently Asked Questions | Set 1 Level - Easy Get minimum element from the stack - Practice hereSerialize and deserialize a binary tree - Practice herePrint a binary tree in a vertical order - Practice hereCelebrity problem - Practice hereLevel order traversalSwap the kth element from starting and from the end position - Practice he
2 min read
Microsoft’s most frequently asked interview questions | Set 2
Microsoft's most asked interview questions | Set 1Level - Easy Celebrity problem - Practice herePrint numbers in given range of bst - Practice hereRoof to leaf path sum - Practice hereLevel order traversal - Practice hereTransform to sum tree - Practice hereDelete middle of linked list - Practice hereK distance from root - Practice hereElements tha
1 min read
Infosys's most asked interview questions
1) Tell me about yourself. 2) About projects, role in project. 3) Why do you want to join Infosys? 4) Strategy to handle major project 5) What is your career objective? 6) Are you fine with working in night shifts? 7) Why should we hire you? 8) They may ask about your family background also. This is a small list of questions. Please write comments
1 min read
Troubleshooting Questions on OS and Networking asked in Cloud based Interview
This article has a Top 5 Troubleshooting questions on Operating System and Networking that will help you clear any Cloud-based Interview like AMAZON CLOUD SUPPORT ASSOCIATE AND AMAZON DevOps. Firstly, what is troubleshooting? Troubleshooting is basically an art, the art of taking a problem into consideration gathering pieces of information about th
3 min read
10 Most Asked ES6 Interview Questions & Answers For Developers
When JavaScript came into the picture of the programming world, the name was chosen for marketing reasons. At that time Java was getting popular all around the world (but you better know both are different). Later it was submitted to ECMA (European Computer Manufacturers Association) to standardize the language and its specification. Later it was n
6 min read
Top Most Asked System Design Interview Questions
System Design is defined as a process of creating an architecture for different components, interfaces, and modules of the system and providing corresponding data helpful in implementing such elements in systems. Table of Content 1. Why is it better to use horizontal scaling than vertical scaling?2. What is sharding, and how does it improve databas
11 min read
Accenture's most asked Interview Questions
1) What is/are your favorite subject(s)? There may be many questions on the subject told. 2) Differences between C and C++. 3) What is include in a C program? 4) What is Dynamic Memory Allocation, example? 5) Differences between C/C++ and Java? 6) Simple programs like Bubble Sort, sum of a simple series, etc. 7) What is OOP? 8) What are encapsulati
1 min read
Crack UX Design Interview: Most Asked UX Design Questions, Tips and Tricks
Planning to change your career or land a job as a UX Designer? We got you sorted. The field of UX design is flourishing. If you've been considering applying for a position as a UX designer at any company/agency, you should take some time to get ready for the interview. There is a lot of preparation that goes into preparing for a design interview. Y
14 min read
Top 50 Most Asked Google Tricky Interview Questions
Google is a dream workplace for every software developer to work in, It is one of the best in every aspect, be it the projects their employees work on or work-life balance. So, to be a part of such an awesome workplace one must be adequately prepared for its Technical and behavioral rounds of the hiring process. Google Interview Process: 1) Online
15+ min read
Microsoft's most asked interview questions
Like other product-based companies, Microsoft also asks data structures and algorithms as part of their technical interview. Below is a list of questions prepared from different Microsoft interview experiences. Most Asked QuestionsCheck if a Binary Tree is BST or not - Practice hereRemove duplicates from a string, do it in-place - Practice hereGive
2 min read
Most asked Computer Science Subjects Interview Questions in Amazon, Microsoft, Flipkart
This article contains a list of most asked questions from Operating Systems, Computer Networks and DBMS in the interviews of the top product based companies like Amazon, Microsoft, Flipkart, Paytm etc.  Operating System: Process Introduction What is a microprocessor?Explain the internal architecture of a RAM.How compiler compiles the interlinked li
3 min read
Top 25 Frequently Asked Interview Questions in Technical Rounds
Here is the collection of the TOP 25 frequently asked questions based on the experience of interviews in multiple companies. 1Lowest common Ancestor2An unsorted array of integers is given; you must find the max product formed by multiplying three numbers. (You cannot sort the array, watch out when there are negative numbers)3Left View of a tree4Rev
1 min read
Most Asked Binary Search Interview Questions
Binary search is the most efficient searching algorithm having a run-time complexity of O(log2 N) in a sorted array. Binary search is a searching technique to search an ordered list of data based on the Divide and Conquer technique which repeatedly halves the search space in every iterationConditions for when to apply Binary Search in a Data Struct
2 min read
SQL Interview Questions asked in Top Tech Companies
1.What do you understand by Adaptive query processing launched in SQL Server? Answer: SQL Server and Azure SQL Database introduce a new generation of query processing improvements that will adapt optimization strategies to your application workload’s runtime conditions. 2.Name all three Adaptive query processing features? Answer. In SQL Server and
4 min read
Most asked Singleton Design Pattern Interview Questions
This article comprises some of the most asked Singleton design pattern interview questions, which will help you tackle any Singleton design pattern question properly. 1. Give an example use case for the singleton design pattern.This design pattern can be useful in various scenarios, such as when you need to control access to a shared resource, mana
11 min read