The Wayback Machine - https://web.archive.org/web/20240928070558/https://www.geeksforgeeks.org/sql-interview-questions-set-2/
Open In App

SQL Interview Questions | Set 2

Last Updated : 16 Jun, 2024
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow
  1. Difference between Locking, Blocking and Deadlocking
    • Locking: Locking occurs when a connection needs access to a piece of data in a database and it locks it for certain use so that no other transaction is able to access it.
    • Blocking: Blocking occurs when a transaction tries to acquire an incompatible lock on a resource that another transaction has already locked. The blocked transaction remains blocked until the blocking transaction releases the lock.
    • Deadlocking: Deadlocking occurs when two or more transactions have a resource locked, and each transaction requests a lock on the resource that another transaction has already locked. Neither of the transactions here can move forward, as each one is waiting for the other to release the lock.
  2. Delete duplicate data from table so that only first data remains constantManagers
    Id Name Salary
    1 Harpreet 20000
    2 Ravi 30000
    3 Vinay 10000
    4 Ravi 30000
    5 Harpreet 20000
    6 Vinay 10000
    7 Rajeev 40000
    8 Vinay 10000
    9 Ravi 30000
    10 Sanjay 50000

    Query:

    DELETE M1 from managers M1, managers M2 where M2.Name=M1.Name AND M1.Id>M2.Id;

    Output:

    Id Name Salary
    1 Harpreet 20000
    2 Ravi 30000
    3 Vinay 10000
    7 Rajeev 40000
    10 Sanjay 50000
  3. Find the Name of Employees where First Name, Second Name, and Last Name is given in the table. Some Name is missing such as First Name, Second Name and maybe Last Name. Here we will use COALESCE() function which will return first Non Null values. Employees
    ID FName SName LName Salary
    1 Har preet Singh 30000
    2 Ashu NULL Rana 50000
    3 NULL Vinay Thakur 40000
    4 NULL Vinay NULL 10000
    5 NULL NULL Rajveer 60000
    6 Manjeet Singh NULL 60000

    Query :

    SELECT ID, COALESCE(FName, SName, LName) as Name FROM employees;

    Output:

  4. Find the Employees who were hired in the Last n months Finding the Employees who have been hire in the last n months. Here we get desired output by using TIMESTAMPDIFF() mysql function Employees
    ID FName LName Gender Salary Hiredate
    1 Rajveer Singh Male 30000 2017/11/05
    2 Manveer Singh Male 50000 2017/11/05
    3 Ashutosh Kumar Male 40000 2017/12/12
    4 Ankita Sharma Female 45000 2017/12/15
    5 Vijay Kumar Male 50000 2018/01/12
    6 Dilip Yadav Male 25000 2018/02/26
    7 Jayvijay Singh Male 30000 2018/02/18
    8 Reenu Kumari Female 40000 2017/09/19
    9 Ankit Verma Male 25000 2018/04/04
    10 Harpreet Singh Male 50000 2017/10/10

    Query:

    Select *, TIMESTAMPDIFF(month, Hiredate, current_date()) as 
    DiffMonth from employees
    where TIMESTAMPDIFF(month, Hiredate, current_date()) between
    1 and 5 order by Hiredate desc;

    Note: Here in query 1 and 5 are indicates 1 to n months which show the Employees who have hired last 1 to 5 months. In this query, DiffMonth is an extra column for our understanding which shows the Nth months. Output:

  5. Find the Employees who hired in the Last n daysFinding the Employees who have been hired in the last n days. Here we get desired output by using DATEDIFF() mysql function Employees
    ID FName LName Gender Salary Hiredate
    1 Rajveer Singh Male 30000 2017/11/05
    2 Manveer Singh Male 50000 2017/11/05
    3 Ashutosh Kumar Male 40000 2017/12/12
    4 Ankita Sharma Female 45000 2017/12/15
    5 Vijay Kumar Male 50000 2018/01/12
    6 Dilip Yadav Male 25000 2018/02/26
    7 Jayvijay Singh Male 30000 2018/02/18
    8 Reenu Kumari Female 40000 2017/09/19
    9 Ankit Verma Male 25000 2018/04/04
    10 Harpreet Singh Male 50000 2017/10/10

    Query:

    select *, DATEDIFF(current_date(), Hiredate)as 
    DiffDay from employees
    where DATEDIFF(current_date(), Hiredate) between
    1 and 100 order by Hiredate desc;

    Note : Here in query 1 and 100 indicates 1 to n days which show the Employees who have hired last 1 to 100 days. In this query DiffDay is an extra column for our understanding which shows the Nth days. Output:

  6. Find the Employees who were hired in the Last n years Finding the Employees who have been hired in the last n years. Here we get desired output by using TIMESTAMPDIFF() MySQL function Employees
    ID FName LName Gender Salary Hiredate
    1 Rajveer Singh Male 30000 2010/11/05
    2 Manveer Singh Male 50000 2017/11/05
    3 Ashutosh Kumar Male 40000 2015/12/12
    4 Ankita Sharma Female 45000 2016/12/15
    5 Vijay Kumar Male 50000 2017/01/12
    6 Dilip Yadav Male 25000 2011/02/26
    7 Jayvijay Singh Male 30000 2012/02/18
    8 Reenu Kumari Female 40000 2013/09/19
    9 Ankit Verma Male 25000 2017/04/04
    10 Harpreet Singh Male 50000 2017/10/10

    Query:

    select *, TIMESTAMPDIFF(year, Hiredate, current_date()) as 
    DiffYear from employees
    where TIMESTAMPDIFF(year, Hiredate, current_date())
    between 1 and 4 order by Hiredate desc;

    Note: Here in query 1 and 4 are indicates 1 to n years which shows the Employees who have hired last 1 to 4 years. In this query, DiffYear is a extra column for our understanding which show the Nth years. Output:

  7. Select all names that start with a given letter Here we get desired output by using three different queries Employees
    ID FName LName Gender Salary Hiredate
    1 Rajveer Singh Male 30000 2010/11/05
    2 Manveer Singh Male 50000 2017/11/05
    3 Ashutosh Kumar Male 40000 2015/12/12
    4 Ankita Sharma Female 45000 2016/12/15
    5 Vijay Kumar Male 50000 2017/01/12
    6 Dilip Yadav Male 25000 2011/02/26
    7 Jayvijay Singh Male 30000 2012/02/18
    8 Reenu Kumari Female 40000 2013/09/19
    9 Ankit Verma Male 25000 2017/04/04
    10 Harpreet Singh Male 50000 2017/10/10

    Query:

    select *from employees where Fname like 'A%';

    select *from employees where left(FName, 1)='A';

    select *from employees where substring(FName, 1, 1)='A';

    Note: Here every query will give same output and the list of Employees who’s FName start with letter A.

Related articles :

  1. SQL Interview Questions | Set 1
  2. Commonly asked DBMS Questions Set 1
  3. Commonly asked DBMS Questions Set 2


Previous Article
Next Article

Similar Reads

Difference between Structured Query Language (SQL) and Transact-SQL (T-SQL)
Structured Query Language (SQL): Structured Query Language (SQL) has a specific design motive for defining, accessing and changement of data. It is considered as non-procedural, In that case the important elements and its results are first specified without taking care of the how they are computed. It is implemented over the database which is drive
2 min read
Configure SQL Jobs in SQL Server using T-SQL
In this article, we will learn how to configure SQL jobs in SQL Server using T-SQL. Also, we will discuss the parameters of SQL jobs in SQL Server using T-SQL in detail. Let's discuss it one by one. Introduction :SQL Server Agent is a component used for database task automation. For Example, If we need to perform index maintenance on Production ser
7 min read
SQL Quiz : Practice SQL Questions Online
This SQL quiz covers various topics like SQL basics, CRUD operations, operators, aggregation functions, constraints, joins, indexes, transactions, and query-based scenarios. We've included multiple-choice questions, fill-in-the-blank questions, and interactive coding challenges to keep things interesting and challenging. Whether you're a beginner l
3 min read
SQL Interview Questions | Set 1
Que-1: Difference between blocking and deadlocking. Blocking: Blocking occurs is a transaction tries to acquire an incompatible lock on a resource that another transaction has already locked. The blocked transaction remain blocked until the blocking transaction releases the lock. Deadlocking: Deadlocking occurs when two or more transactions have a
3 min read
SQL Server Interview Questions
Are you preparing for an SQL Server interview? Preparing for a SQL Server interview requires a comprehensive understanding of SQL concepts, ranging from basic to advanced levels. Whether you're a seasoned professional or a beginner, it's essential to review and practice common interview questions to ensure readiness. In this interview preparation g
15+ min read
Amazon SQL Interview Questions
In an Amazon SQL interview, candidates can expect a range of questions that test their SQL knowledge and problem-solving skills. These questions often involve topics such as data manipulation, query optimization, and database design. To succeed, candidates should be well-versed in SQL fundamentals and prepared to solve complex scenarios with confid
12 min read
Spotify SQL Interview Questions
Spotify is a popular music streaming platform that uses data analysis and management to improve user experience and provide personalized content. Spotify heavily relies on SQL (Structured Query Language) to manage its vast database and derive valuable insights. Whether you're preparing for a job interview at Spotify or aiming to sharpen your SQL sk
11 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
Advanced SQL Interview Questions
When preparing for a data-centric role, mastering advanced SQL interview questions is an important step. SQL (Structured Query Language) is the backbone of database management, and employers look for candidates who can show they understand advanced SQL concepts. This guide will cover some of the most common advanced SQL interview questions and give
4 min read
Google SQL Interview Questions
Google is known for its challenging technical interviews and SQL is a key component of these interviews, especially for roles related to data analysis, database engineering, and backend development. Google SQL interview questions typically focus on candidates' proficiency in writing complex SQL queries, optimizing database operations, and demonstra
11 min read
PL/SQL Interview Questions and Answers
PL/SQL (Procedural Language for SQL) is one of the most widely used database programming languages in the world, known for its robust performance, flexibility, and tight integration with Oracle databases. It is a preferred choice for many top companies such as Oracle, IBM, Accenture, Infosys, TCS, Cognizant, and many more due to its powerful featur
15 min read
SQL Interview Questions
SQL is a standard database language used for accessing and manipulating data in databases. It stands for Structured Query Language and was developed by IBM Computer Scientists in the 1970s. By executing queries, SQL can create, update, delete, and retrieve data in databases like MySQL, Oracle, PostgreSQL, etc. Overall, SQL is a query language that
15+ min read
Top 50 SQL Questions For Data Analyst Interview
SQL interview questions for data analysts often cover a range of topics, from basic querying techniques to advanced data manipulation and performance optimization. These questions are designed to assess a candidate's understanding of SQL concepts, their ability to write and optimize queries, and their problem-solving skills when working with real-w
10 min read
Top 15 Uber SQL Interview Questions with Answers and Explanations
Preparing for a SQL interview at Uber involves not only understanding SQL concepts but also being able to work with practical examples. Here, we'll provide 15 unique SQL questions you might encounter during an interview at Uber, along with detailed database setups and explanations. Each question will include the query, output, and thorough explanat
12 min read
SQL Query Interview Questions
SQL or Structured Query Language is a standard language for relational databases. SQL queries are powerful tools used to, manipulate, and manage data stored in these databases like MySQL, Oracle, PostgreSQL, etc. Whether you're fetching specific data points, performing complex analyses, or modifying database structures, SQL queries provide a standa
10 min read
Data Engineer Interview Questions: Top 60 Plus Questions and Answers for 2024
Data engineering is a rapidly growing field that plays a crucial role in managing and processing large volumes of data for organizations. As companies increasingly rely on data-driven decision-making, the demand for skilled data engineers continues to rise. If you're preparing for a data engineer interview, it's essential to be well-versed in vario
15+ min read
Kafka Interview Questions - Top 70+ Questions and Answers for 2024
Apache Kafka has become a cornerstone in modern distributed systems and data-driven architectures. As organizations increasingly adopt real-time data streaming and processing capabilities, the demand for professionals skilled in Kafka has surged. Whether you're a software engineer, data engineer, or DevOps specialist, a strong understanding of Kafk
15+ min read
Active Directory Interview Questions - Top 50+ Questions and Answers for 2024
Active Directory (AD) is a crucial component of modern enterprise IT infrastructure, providing centralized authentication, authorization, and directory services for Windows-based networks. As organizations continue to rely heavily on AD for managing user identities, access controls, and network resources, the demand for skilled AD administrators an
15+ min read
Teacher Interview Questions - Top 70 Questions and Answers for 2024
Teaching is a noble profession that requires a unique blend of knowledge, skills, and passion. As educators, teachers play a crucial role in shaping the minds of future generations, fostering critical thinking, and nurturing the potential of each student. In today's rapidly evolving educational landscape, teachers must be prepared to meet diverse c
15+ min read
SQL SERVER – Input and Output Parameter For Dynamic SQL
An Input Parameter can influence the subset of rows it returns from a select statement within it. A calling script can get the value of an output parameter. An aggregate function or any computational expression within the stored process can be used to determine the value of the output parameter. A parameter whose value is given into a stored proced
3 min read
Difference between T-SQL and PL-SQL
1. Transact SQL (T-SQL) : T-SQL is an abbreviation for Transact Structure Query Language. It is a product by Microsoft and is an extension of SQL Language which is used to interact with relational databases. It is considered to perform best with Microsoft SQL servers. T-SQL statements are used to perform the transactions to the databases. T-SQL has
3 min read
SQL Server | Convert tables in T-SQL into XML
In this, we will focus on how tables will be converted in T-SQL into XML in SQL server. And you will be able to understand how you can convert it with the help of command. Let's discuss it one by one. Overview :XML (Extensible Markup Language) is a markup language similar to HTML which was designed to share information between different platforms.
2 min read
SQL - SELECT from Multiple Tables with MS SQL Server
In SQL we can retrieve data from multiple tables also by using SELECT with multiple tables which actually results in CROSS JOIN of all the tables. The resulting table occurring from CROSS JOIN of two contains all the row combinations of the 2nd table which is a Cartesian product of tables. If we consider table1 contains m rows and table2 contains n
3 min read
How to Execute SQL Server Stored Procedure in SQL Developer?
A stored procedure is a set of (T-SQL ) statements needed in times when we are having the repetitive usage of the same query. When there is a need to use a large query multiple times we can create a stored procedure once and execute the same wherever needed instead of writing the whole query again. In this article let us see how to execute SQL Serv
2 min read
SQL Query to Check if Date is Greater Than Today in SQL
In this article, we will see the SQL query to check if DATE is greater than today's date by comparing date with today's date using the GETDATE() function. This function in SQL Server is used to return the present date and time of the database system in a ‘YYYY-MM-DD hh:mm: ss. mmm’ pattern. Features: This function is used to find the present date a
2 min read
SQL Query to Add a New Column After an Existing Column in SQL
Structured Query Language or SQL is a standard Database language that is used to create, maintain and retrieve data from relational databases like MySQL, Oracle, SQL Server, Postgres, etc. In Microsoft SQL Server, we can change the order of the columns and can add a new column by using ALTER command. ALTER TABLE is used to add, delete/drop or modif
3 min read
SQL Query to Convert Rows to Columns in SQL Server
In this article we will see, how to convert Rows to Column in SQL Server. In a table where many columns have the have same data for many entries in the table, it is advisable to convert the rows to column. This will help to reduce the table and make the table more readable. For example, Suppose we have a table given below: NAMECOLLEGEROLL NUMBERSUB
2 min read
How to SQL Select from Stored Procedure using SQL Server?
There may be situations in SQL Server where you need to use a stored procedure to get data from a SQL query. For direct data selection from a stored procedure within a query, SQL Server offers options like OPENQUERY and OPENROWSET. The usual way is running the stored procedure independently and then querying the outcomes. The idea of utilizing SQL
3 min read
BULK INSERT in SQL Server(T-SQL command)
BULK INSERT in SQL Server(T-SQL command): In this article, we will cover bulk insert data from csv file using the T-SQL command in the SQL server and the way it is more useful and more convenient to perform such kind of operations. Let's discuss it one by one.  ConditionSometimes there is a scenario when we have to perform bulk insert data from .cs
3 min read
SQL Exercises : SQL Practice with Solution for Beginners and Experienced
SQL (Structured Query Language) is a powerful tool used for managing and manipulating relational databases. Whether we are beginners or experienced professionals, practicing SQL exercises is important for improving your skills. Regular practice helps you get better at using SQL and boosts your confidence in handling different database tasks. So, in
15+ min read