LogoLogo

SQL Interview Questions

Prasun Das| July 13, 2022 at 3:06 PM | 3 minutes Read

Are you getting ready for your SQL developer job interview?


You've come to the correct place.


This tutorial will assist you in brushing up on your SQL skills, regaining confidence, and being job-ready!


Here is a collection of real-world interview questions from organisations such as Google, Oracle, Amazon, and Microsoft, among others. Each question is accompanied by a neatly written answer inline, saving you time during interview preparation.



Following are some practise problems to help you master the fundamentals of SQL.



1. Find the third highest salary from the Employee table.


SELECT * FROM EMPLOYEE ORDERBY salary DESCLIMIT2,1;
SELECT salary FROM EMPLOYEE ORDERBY salary DESCLIMIT2,1;


Note: We pass two parameters to the LIMIT clause, where the first one is the offset that is after which row data needs to be fetched and the second parameter is the row count that is how many rows it has to return. Here we need 1 row from after the 2nd row, hence LIMIT 2,1.


2. Display the first and last record from the Employee table.

For first record:

SELECT * FROM EMPLOYEE WHERE EMPID=(SELECTMIN(EMPID) FROM EMPLOYEE);


For last record:

SELECT * FROM EMPLOYEE WHERE EMPID=(SELECTMAX(EMPID) FROM EMPLOYEE);

Here subqueries has been used to get the minimum and maximum value of EMPID that is the first and last records and returned to the outer query respectively.


3. Display the list of employees working in the same department.

SELECTDISTINCT E.EMPID, E.EMPNAME, E.DEPARTMENT FROM EMPLOYEE E, EMPLOYEE E1
WHERE E.DEPARTMENT = E1.DEPARTMENT AND E.EMPID!=E1.EMPID;

From the two tables E and E1 distinct employees are selected and returned the ones having the same department but different empID.


4. Retrieve the last 3 records from the Employee table.

SELECT * FROM (SELECT * FROM EMPLOYEE ORDERBY EMPID DESCLIMIT3) temp
ORDERBY EMPID ASC;

Output of inner query is hold by the cursor temp then is returned to the outer query which rearranges the 3 records in ascending order.


5. Show details of employees whose EmpLastName ends with the alphabet ‘S’ and contains 4 alphabets.

SELECT * FROM EMPLOYEE WHERE EmpLastName LIKE ‘___s’;

Three underscore followed by s gives the last names ending with s and having total 4 letters.


6. Fetch the duplicate rows in a table.

SELECT *, COUNT(empID) FROM EMPLOYEE GROUPBY empID HAVINGCOUNT(empID)>1;

Here the duplicates is found by calculating the count of each empID and returning the ones present more than once.


7. Calculate even and odd records from a table.

For Even:

SELECT * FROM EMPLOYEE WHEREMOD(EMPID,2)=0;

For Odd:

SELECT * FROM EMPLOYEE WHEREMOD(EMPID,2)=1;

MOD function is used for getting reminder, where the first parameter is the dividend and the second is divisor.


8. Write a query to retrieve first four characters of employee name.

SELECTSUBSTRING (name,1,4) FROM EMPLOYEE;

Substring is a function that is used to get the parts of a string where the first parameter is the column name, followed by starting and ending positions respectively.


9. Write a query to return the number of employees whose DOB is between 01/01/1990 to 01/01/2001 and are grouped according to gender.

SELECTCOUNT(*), Gender FROM EMPLOYEE
WHERE DOB BETWEEN ‘1990-01-01’ AND ‘2001-01-01’
GROUPBY Gender;


If we pass date in DD/MM/YYYY format then we need to specify it accordingly into the query:


SELECTCOUNT(*), Gender FROM EMPLOYEE
WHERE DOB BETWEENSTR_TO_DATE(‘01-01-1990’,’%d/%m/%Y’) ANDSTR_TO_DATE(‘01-01-2001’,’%d/%m/%Y’)
GROUPBY Gender;


10. Fetch the details of employees excluding the ones who are ‘HR’ and ‘ADMIN’.

SELECT * FROM EMPLOYEE WHERE Department NOTIN (‘HR’,’ADMIN’);

Here the NOT IN operator has been used to filter the data from Department that is not having values as HR or Admin.


11. Display the total salary of each employee after adding 10% increment in the salary.

SELECTID,name, salary+(salary/10) asNew Salary FROM EMPLOYEE.

We perform the required arithmetic operations to get the desired result in a new column as New Salary.


12. Fetch the order details of today’s date. Given is the orders table with the order_id and their corresponding dates as order_Updated_date.

SELECT * from ordersTable whereDate(order_Updated_date) = current_date;


Date function is used to compare the order date and today’s date.



This blog gives you a rough pathway of core questions ans topics to follow. Ofcourse, you can and should explore more on your journe. Also, if you have any particular topic that you want covered, please write to us. If you wanna connect with me, here is my linkedin profile — Sneha Biswas. If you have any questions, feel free to contact me.

#sql#interview preparation#dbms#fundamentals to learn
View Count:4.8k
12

Comments

Similar Articles

PWC Placement Experince

PricewaterhouseCooper Pvt Ltd (PwC Pvt Ltd) is a leading multinational professional service network of firms, operating as partnerships under the PwC ......

Cover image

DSA Cheatsheet Curated list (Leetcode)

If you're looking to prepare for a job in the tech industry, Data Structures and Algorithms (DSA) are essential to master. Practising problems on plat......

Cover image

How to Start with DSA

Each and every programmer needs a strong grasp in DSA to write efficient and optimised codes. However, DSA has a reputation of being one of the most f......

Cover image

Campus Placement Roadmap for Batch 2024

Its that time round the calendar again, but different for you all as for the first time now. You all will be facing the on campus placement season.Wit......

Cover image

Busting myths about web3

WHAT IS WEB3?Web3 is the catch-all term for the vision of an upgraded version of the internet aimed at giving power back to the users. It is truly the......

Cover image

Operating System INTERVIEW QUESTIONS

Operating Systems is one of the core subjects for a computer science student. As such a lot of important questions are asked on this subject in interv......

Cover image

Cognizant previous year QnA and Interview Experience

Cognizant helps organizations remain ahead of the competition by modernizing technology, reinventing processes, and redefining customer experiences. I...

Cover image

Cloud Computing Cheatsheet

Getting started with cloud computing ?The words "cloud" and "web" will be interchangeable. What kind of business operates today without the internet? ...

Cover image

Software Engineering — SOFTWARE MAINTENANCE [Part-4]

SOFTWARE MAINTENANCE:Software Maintenance can be defined as the process of modifying a software product after its delivery. The main purpose of softwa...

Cover image

Software Engineering — Software Process Activities [Part - 3]

A quick summary upto what we did this far  from this series:Software EngineeringSoftware engineering is a discipline of engineering concerned wit...

Cover image

Capgemini Previous Year Questions with Study Materials

Capgemini is a global leader in consulting, digital transformation, technology, and engineering services. In the rapidly evolving world of cloud, digi...

Cover image

MINDTREE Interview Experience and Previous Year Questions Part 2

Technical Round :Candidates who pass the online test will be invited to the technical interview...

Cover image

MINDTREE Interview Experience and Previous Year Questions Part 1

About Mindtree:Mindtree Ltd, located in Bangalore, India, is a prominent Indian multinational information technology and outsourcing company. The&nbsp...

Cover image

TCS NQT Latest Questions and Answers

TCS NQT Interview kicks off with the aptitude test. The test follows the following pattern : TCS uses the TCS-iON software for their online aptit...

Cover image

INTERVIEW TIPS AND TRICKS FOR FRESHERS

Increased competition for fewer jobs in the current economy means that just being right for the role on paper, is not enough on its own anymore. You h...

Cover image

TCS NINJA INTERVIEW EXPERIENCE [2023]

About TCS NinjaTCS administers the NQT to select candidates for a variety of positions (National Qualifier Test). Tens of thousands of people apply fo...

Cover image

Understanding Time Complexity with Interview Examples

Introduction:Understanding time complexity of a function is imperative for every programmer to execute a program in the most efficient manner. Time co...

Cover image

Software Engineering — Software Development lifecycle and software process(Part 2)

 Software MethodologyA software methodology (which is also known as software process) is a set of related development Phases that leads to the pr...

Cover image

TCS Digital Interview Experience [2023]

About TCS DigitalTCS selects applicants for a variety of jobs by conducting the NQT (National Qualifier Test). About 10,000-15,000 job applicants subm...

Cover image

Software Engineering [Part 1] - Introduction

Software Engineering is a subject that requires when you are a software developer as well as when you are in your college preparing for a semester or ...

Cover image

SQL CHEATSHEET

Introduction: What is SQL?To understand SQL, we must first understand databases and database management systems (DBMS).Data is essentially a collectio...

Cover image

TCS NQT Technical Interview Questions

TCS Recruitment ProcessInterview RoundsInterview round 1:  TCS NQTTCS NQT (National Qualifier T...

Cover image

TCS NQT Aptitude Questions

TCS NQT Aptitude has three sections, namely the Numerical Ability, Verbal Ability, and Reasoning Abi...

Cover image

TCS NQT Interview Experience [2023]

About TCS NQTTCS NQTs, also known as TCS National Qualifier Tests or Tata Consultancy Services Natio...

Cover image

Computer Networking Cheatsheet (Last Minute Notes)

 Networking: Importance in real life!Using a computer for work, as well as for personal use, ha...

Cover image

Database Management System Last Minute Notes [Part - 2]

ER-DiagramWhat are ER Models ?An Entity Relationship Diagram (ERD) is a visual representation of dif...

Cover image

Database Management System Last Minute Notes [Part - 1]

What is actually DBMS?A Database Management System (DBMS) is system software used to manage the orga...

Cover image

Final year Placement Roadmap in India

Nowadays students tend to go more for off campus placements rather than on campus placements.One of ...

Cover image

C Cheatsheet

C CHEATSHEETINTRODUCTION:C programming language was developed by Dennis Ritchie of Bell Labs in 1972...

Cover image

C Interview Questions — 2022

C is a general purpose high-level language most popular amongst coders, it is the most compatible, e...

Cover image

Cognizant GenC Next Interview

What is GenC and GenC Next?GenC stands for Generation Cognizant. It basically means the fresher hiri...

Cover image

C++ STL Cheat Sheet

C++ programming language has templates which basically allows functions and classes to work with gen...

Cover image

C++ CHEAT SHEET

C++ was conceived in the late 1970s to overcome the limitations of C. It  is an extension of th...

Cover image

Python [Advanced] Interview Questions — 2022

Interview Questions are a great source to test your knowledge about any specific field. But remember...

Cover image

Basic Python [Core] Interview Questions for Freshers and Short Sample Answers — 2022

The most popular high-level, multipurpose programming language right now is Python.Python supports p...

Cover image

Python Cheat Sheet - Learn the basics of Python

IntroductionPython is a high-level programming language with dynamic binding and high-level inherent...

Cover image

Basic Java Interview Questions for Freshers and Short Sample Answers — 2022

Interview Questions are a great source to test your knowledge about any specific field. But remember...

Cover image

Java OOP Cheat Sheet — A Quick Guide to Object-Oriented Programming in Java

Object-Oriented Programming or better known as OOPs is one of the major pillars of Java that has lev...

Cover image

Learn Java: Basics to Advanced Concepts [Java Cheatsheet]

Java is a high-level programming language known for its robustness, object-oriented nature, enhanced...

Cover image

A Complete Roadmap for On-Campus Placements

Placements are an important part of engineering life, and while everyone hopes to be placed in a reputable company, only a few are successful. This is primarily due to a...

Cover image