Practice SQL Interview questions and Answers

 

There’s no better way to improve your SQL skills than to practice with some real SQL interview questions – and these SQL practice problems are a great way to improve your SQL online. We recommend first creating the following simple tables presented below in the RDBMS software of your choice – MySQL, Oracle, DB2, SQL Server, etc, and then actually try to figure out the answer on your own if possible.

The following SQL practice exercises were actually taken from real interview tests with Google and Amazon. Once again, we highly recommended that you try finding the answers to these SQL practice exercises on your own before reading the given solutions. The practice problems are based on the tables presented below.

Salesperson Customer
ID Name Age Salary
1 Abe 61 140000
2 Bob 34 44000
5 Chris 34 40000
7 Dan 41 52000
8 Ken 57 115000
11 Joe 38 38000
ID Name City Industry Type
4 Samsonic pleasant J
6 Panasung oaktown J
7 Samony jackson B
9 Orange Jackson B
Orders
Number order_date cust_id salesperson_id Amount
10 8/2/96 4 2 540
20 1/30/99 4 8 1800
30 7/14/95 9 1 460
40 1/29/98 7 2 2400
50 2/3/98 6 7 600
60 3/2/98 6 7 720
70 5/6/98 9 7 150

Given the tables above, find the following:

a. The names of all salespeople that have an order with Samsonic.

b. The names of all salespeople that do not have any order with Samsonic.

c. The names of salespeople that have 2 or more orders.

d. Write a SQL statement to insert rows into a table called highAchiever(Name, Age), where a salesperson must have a salary of 100,000 or greater to be included in the table.

Subscribe to our newsletter for more free interview questions.

Let’s start by answering part a. It’s obvious that we would need to do a SQL join, because the data in one table will not be enough to answer this question. This is a good question to get some practice with SQL joins, so see if you can come up with the solution.

Now, what tables should we use for the join? We know that the customer ID of Samsonic is 4, so we can use that information and do a simple join with the salesperson and customer tables. The SQL would look like this:

select Salesperson.Name from Salesperson, Orders where 
Salesperson.ID = Orders.salesperson_id and cust_id = '4';

We can also use subqueries (a query within a query) to come up with another possible answer. Here is an alternative, but less efficient, solution using a subquery:

select Salesperson.Name from Salesperson where 
Salesperson.ID = '{select Orders.salesperson_id from Orders, 
                   Customer where Orders.cust_id = Customer.id
                   and Customer.name = 'Samsonic'}';

Click on the Next button below to check out the answer to parts B and C of this SQL interview question.

Visit our FORUM - ask/answer programming-related questions!>>

Reach 200,000 programmers, POST your JOB on our JOB BOARD for only $3.00 DOLLARS!

Subscribe to our newsletter for more free interview questions.


Varoon Sahgal, Author of ProgrammerInterviewon
  • ceecee17

    Hello! Thought I should share a quiz I was given in an interview. Feel free to post solutions to the quiz, however I already submitted my answers and Im sure I failed

    Candidate _____________________________________________

    Sample Tables:

    employee (Table)

    emp_ID fname lname dept_ID

    ———– —— ——- ———–

    1 John Doe 3

    2 Jane Doe 2

    3 Ron Smith 3

    4 Nancy King 4

    5 James Monroe 1

    6 Lisa Jones 0

    department (Table)

    dept_ID dept_name

    ———– ——————————

    1 Sales

    2 Customer Service

    3 Accounting

    4 Marketing

    5 Purchasing

    Write the SQL required
    to do the following:

    1.
    Add a new department named Janitorial Services whose department ID is 6.

    2.
    Modify the database to reflect that Ron Smith has changed jobs and now works in
    the Sales Department.

    3.
    Retrieve the last name and department name of all employees whose last name
    begins with A through J.

    • Varoon

      Thanks for the post!

  • Californian

    Dear Sql gurus:

    I have a series of numbers in my data source and all I want is the second digit to start with one when I search in my phpMyadmin area;

    It’s like this before:

    016340500000000000000000000
    006899400000000000000000000
    037898400000000000000000000

    and I want to find only 1′s in the second digit of these numbers.

    Can anything be done to achieve this?

    Thanks

    Californian