Top SQL Queries to practice for Interviews

During interviews , instead of directly asking questions on theoretical concepts, now a days interviewers ask SQL queries that test both the theoretical as well as practical understanding and usage of the different SQL concepts.

In this post, we discuss some of the most common SQL queries for the interviews that you should prepare. So, let’s begin.

Q1. Write a query to update a field’s values by removing any leading and trailing spaces.

 Ans.  UPDATE command with LTRIM() and RTRIM() functions. UPDATE Table1 SET Name = LTRIM(RTRIM(Name));


Q2. Write an SQL query to return the count of occurrence of a character in a particular field.

 Ans. For this query, we will use the Length() function with the subtraction operator and aReplace function. 

SELECT Name, LENGTH(Name) - LENGTH(REPLACE(Name, 'k', '')) FROM Employee;


Q3. How can we create a new table with data and structure the same as an existing table?

 Ans. Creating a new table just requires us to use the CREATE TABLE command that supports this when used along with the SELECT command. 

CREATE TABLE TableNew SELECT * FROM TableOld;

Q.4. Write a query to display all records that are present in one table - TableA but are not present in another table - TableB. 

Ans. To display the records from one table not present in another table, we can use the MINUS operator. 

SELECT * FROM TableA MINUS SELECT * FROM TableB;

Q5. Write an SQL query to return the nth highest record  from a table.

 Ans.  we can use the Top command. SELECT TOP 1 Salary FROM (SELECT DISTINCT TOP N Salary FROM TableA ORDER BY Salary DESC) ORDER BY Salary ASC; 

Q.6. Write an SQL query to return all the records lying in a particular year e.g. 2021. 

Ans. we will use the BETWEEN operator in SQL. 

SELECT * FROM TableA WHERE DateJoin BETWEEN '2021/01/01' AND '2021/12/31';

Q.7. Write a query to display only the odd records from a Table

. Ans. For returning the odd records, we can use the MOD function with the ‘not equal to operator’ - <>. SELECT * FROM Employee WHERE MOD (EmpId, 2) <> 0;

Reference: sql interview queries

Comments

Popular posts from this blog

Sample Test Cases for Pen

Top 5 manual testing interview questions

how to write test cases for a Login page