Table of contents
No headings in the article.
Query is a request for something. But in the world of computer programming query means getting data from the database with commands that are preset. i.e. INSERET, SELECT, SUM, WHERE etc. SQL Queries are asked directly in interview as it tests both your practical as well as theoretical knowledge. Do check out the references for best of SQL Queries Questions. Here are the top 10 SQL queries for freshers and for more practice check the references.
![SQL DAta table.JPG] (cdn.hashnode.com/res/hashnode/image/upload/.. align="left")
Que-1 Write an SQL query to fetch the count of employees working in project ‘P1’.
Ans We will use aggregate function count with SQL where clause-
SELECT COUNT(*)
FROM EmployeeSalary
WHERE Project = 'P1';
Que-2 Write an SQL query to find the employee id whose salary lies in the range of 9000 and 15000.
Ans 'Between' operator is used to specify whether the value is in range or not.
SELECT EmpId, Salary
FROM EmployeeSalary
WHERE Salary BETWEEN 9000 AND 15000;
Que-3 Write an SQL query to display the total salary of each employee adding the Salary with Variable value.
Ans Here, we will be using '+' operator in SQL.
SELECT EmpId,
Salary+Variable as TotalSalary
FROM EmployeeSalary;
Que-4 Write an SQL query to fetch the EmpIds that are present in both the tables – ‘EmployeeDetails’ and ‘EmployeeSalary.
Ans Sub Query will be used to fetch the EmpIds from both of the tables.
SELECT EmpId FROM
EmployeeDetails
where EmpId IN
(SELECT EmpId FROM EmployeeSalary);
Que-5 Write an SQL query to fetch the employee full names and replace the space with ‘-’.
Ans 'Replace' function is used to replace the characters given in a text string
SELECT REPLACE(FullName, ' ', '-')
FROM EmployeeDetails;
Que-6 Write an SQL query to update the employee names by removing leading and trailing spaces.
Ans We can remove heading and trailing spaces with the commands 'LTRIM' and 'RTRIM' and can update it using 'Update' command.
UPDATE EmployeeDetails
SET FullName = LTRIM(RTRIM(FullName));
Que-7 Write an SQL query to fetch all employee records from EmployeeDetails table who have a salary record in EmployeeSalary table.
Ans With the help of 'Exists' command
SELECT * FROM EmployeeDetails E
WHERE EXISTS
(SELECT * FROM EmployeeSalary S
WHERE E.EmpId = S.EmpId);
Que-8 Write an SQL query to remove duplicates from a table without using a temporary table.
Ans Using delete with alias and inner join. Now we will check for the equality for every matching record and then remove the row with high EmpId.
DELETE E1 FROM EmployeeDetails E1
INNER JOIN EmployeeDetails E2
WHERE E1.EmpId > E2.EmpId
AND E1.FullName = E2.FullName
AND E1.ManagerId = E2.ManagerId
AND E1.DateOfJoining = E2.DateOfJoining
AND E1.City = E2.City;
Que-9 Write an SQL query to create a new table with data and structure copied from another table.
Ans
CREATE TABLE NewTable
SELECT * FROM EmployeeSalary;
Que-10 Write an SQL query to create an empty table with the same structure as some other table.
Ans The same query that is used above can be used with False 'WHERE' condition
CREATE TABLE NewTable
SELECT * FROM EmployeeSalary where 1=0;
This is the end of our top 10 questions that are mostly asked in interviews. Thanks for reading. :)
REFERENCES:-