Home / mySQL / SQL Query Interview Questions with Answers

SQL Query Interview Questions with Answers

Hi all,
Today I share one important topic related to SQL. SQL query is very important. This article mainly focus sql query related to interview questions and answer.

In this article, I am giving some examples of SQL queries with answer which is frequently asked when you go for a programming interview.

The main advantages of sql queries are: retrieve large amount of data from database very fast, the high speed is positive advantage in SQL queries.

One disadvantage are difficulty to interface and SQL database is more complex.

  1. What is primary key ?

    Answer:A primary key is a special relational database table column designated to uniquely identify all table records.
    The main features of primary key’s are:

    • It must contain a unique value for each row of data.
    • It cannot contain null values.

  2. What is a Foreign Key ?

    Answer:A foreign key is a single column or a multiple columns defined to have values that can be mapped to a primary key in another table.
    A FOREIGN KEY in one table points to a PRIMARY KEY in another table.

  3. SQL query to fetch all the employee Details from EmployeeDetails table

    Answer:Fetch all details from EmployeeDetails table:

    select * from EmployeeDetails;

    EmployeeID Salary
    1 4000
    2 8000
    3 8000
    4 14000
  4. SQL query to fetch EmployeeID from EmployeeDetails table:

    Answer:List all EmployeeID from EmployeeDetails table

    Select EmployeeID from EmployeeDetails;

  5. SQL query to fetch unique Salary from EmployeeDetails table

    Answer:For display unique values from table using DISTINCT keyword in sql query.

    select distinct Salary from EmployeeDetails

  6. Select first 3 characters of Salary from EmployeeDetails table

    Answer:Oracle Equivalent of SQL Server SUBSTRING is SUBSTR, Query.

    select substr(Salary,0,3) from EmployeeDetails;

  7. SQL Query to find second highest salary of EmployeeDetails:

    Answer: There are many ways to find second highest salary of EmployeeDetails in SQL, you can either use SQL Join or Subquery to solve this problem.
    Here is SQL query using Subquery:

    SELECT MAX(Salary) FROM EmployeeDetails WHERE Salary NOT IN (SELECT MAX(Salary) FROM EmployeeDetails);

    Running the SQL above would return us 8500, which is of course the 2nd highest salary in the EmployeeDetails table.
    Code Explanation:The SQL above, first find the highest salary values from the EmployeeDetails table using “Select MAX(Ssalary) from EmployeeDetails”. Then, adding the “WHERE Salary NOT IN” in front basically creates a new set of Salary values that does not include the highest Salary value. For instance, if the highest Salary in the EmployeeDetails table is 14000 then that value will be excluded from the results using the “NOT IN” operator, and all values except for 14000 will be retained in the results.

  8. SQL query to find the lowest salary from the table EmployeeDetails

    Answer:Running the SQL below would return us 4000, which is of course the 2nd highest salary in the EmployeeDetails table

    Select MIN(Salary) from EmployeeDetails;

  9. SQL Query to display the current date.

    Answer:SQL inbuilt function GetDate() display current date.

    SELECT GetDate();

  10. SQL Query to check whether date passed to Query is the date of given format or not.

    Answer:SQL has IsDate() function which is used to check passed value is a date or not of specified format, it returns 1(true) or 0(false) accordingly.

    SELECT ISDATE(‘1/08/13’) AS “MM/DD/YY”;

    It will return 0 because passed date is not in correct format.

  11. SQL Query to display the EmployeeID whose Salary is between 8000 to 14000.

    Answer:This SQL query is tricky, but you can use BETWEEN clause to get all records whose salary fall between two salary.

    SELECT EmployeeID FROM EmployeeDetails WHERE Salary BETWEEN ‘8000’ AND ‘14000’;

  12. SQL Query to find an EmployeeID whose Salary is equal or greater than 10000 from EmployeeDetails table.

    SELECT EmployeeID FROM EmployeeDetails WHERE Salary>=10000;

  13. SQL query to select dates between two dates (2011/02/25′ to ‘2011/02/27)

    select Date from exam where Date between ‘2011/02/25’ and ‘2011/02/27’;


    select Date from exam where Date >= ‘2011/02/25’ and Date <= '2011/02/27'

If anyone has doubts on this topic then please do let me know by leaving comments or send me an email.

Leave a Reply

Your email address will not be published. Required fields are marked *