You are on page 1of 14

SQL Exercises for Beginners / Simple SQL Exercises with Answers / SQL

Exercises for simple Table creation and SELECT queries

Question:
[A] Table Creation
Create a table according to the schema given below. Choose the appropriate data
types while creating the table. Insert the records given in Table 1 into the
Employee table. And, write SQL queries to satisfy the questions given below.
Employee (Emp_ID, Emp_Name, DoB, Department, Designation, DoJ, Salary)
Here, DoB means Date of Birth, DoJ means Date of Joining.
Emp_I Emp_Name DoB Department Designation DoJ Salary
D
F110 Sam 15-JUN- Bio-Technology Professor 12-APR-2001 45000
1970
F111 Kumar 25-MAY- Mechanical Asst. Prof. 02-MAY- 30000
1980 2006
F115 Raguvaran 10-AUG- CSE Asst. Prof. 05-MAY- 27000
1982 2007
F114 Jennifer 10-SEP-1975 CSE Asst. Prof. 03-JUN- 35000
2004
F117 Ismail 15-MAY- IT Asst. Prof. 10-MAY- 33000
1979 2005
Table 1 – Employee

[B] Queries
1. Display all the records from table Employee.
2. Find all the employees who are working for CSE department.
3. Get the details about the employees who have joined after ’10-JUN-2005’.
4. Find all the employees who earn more than 30000.
5. Get the details of employees who are not ‘Professor’.
6. Find the name, date of birth, and designation of all the employees who work for
‘IT’ department.
7. Find all the departments which are offering salary above 25000.
8. Get the DoB of employee named ‘Kumar’.
9. Find the names and departments of employees who earn the salary in the range
20000 to 40000.
10. Find the employee details of any employee who work for ‘CSE’ and earn more
than 30000.
Answers:
[A] Table Creation
The following CREATE TABLE statement will create the table according to the
specification given in question.
CREATE TABLE Employee (
Emp_ID CHAR(3),
Emp_Name VARCHAR(30),
DOB DATE,
Department VARCHAR(20),
Designation VARCHAR(15),
DoJ DATE,
Salary Number(10,2));

Explanation:
Whatever mentioned in CAPITAL LETTERS are keywords/reserved words.
 various datatypes are used for various attribute declaration.
 CHAR data type used for Emp_ID attribute - known the exact size of any
employee ids.
 VARCHAR data type used for Emp_Name, Department, and Designation
attributes, because the values stored in those columns may vary. That is, for
example, a name would be just 3 characters long or 30 characters long.
 DATE data type used for all the attributes - to store dates (DoB, and DoJ).
 NUMBER is used for salary to store salary values. In the above declaration (10,
2) means that any values upto the size of total 10 digits out of which 2 digits
after decimal point.
The CREATE TABLE statement is terminated with ‘;’ as any other statements in
SQL.

Data Insertion
All the records given in Table 1 can be inserted using the following INSERT INTO
statement.
INSERT INTO Employee VALUES (‘F110’, ‘Sam’, ‘15-JUN-1970’, ‘Bio-Technology’,
‘Professor’, ‘12-APR-2001’, 45000);
As stated above, the words in CAPITAL LETTERS are keywords/reserved words.
Observe that how the information are specified for every column. You must
mention the values in the order in which you have declared the attributes in the
table, with the following simple rules;
CHAR, VARCHAR, and DATE attribute values should be given inside a pair of
single quotes.
NUMBER inputs can be specified without any quotes.

[B] Queries
Before go into SELECT queries, recall the structure of a SELECT query. Basically,
we need at least two clauses, SELECT and FROM to write a complete query. The
questions given above need the clauses, SELECT, FROM, and WHERE. See below for
the clauses with the parameters needed;
SELECT */list of attributes to be included in the result
FROM list of one or more tables
WHERE list of conditions ANDed, ORed, or Negated (NOT)

1. Display all the records from table Employee.


For question 1, we need to write a query that displays the entire table. For writing
this query, we need only two clauses, SELECT, and FROM as follows;
SELECT * FROM Employee;
The parameter for SELECT clause, ‘*’ represents all the columns/attributes of the
table parameter which is given in FROM clause. You may write the query in single
line also.
The result is Table 1 as it is.
2. Find all the employees who are working for CSE department.
Question 2 includes a condition. The condition is ‘the employees working for CSE
department’. Hence, we have a parameter for WHERE clause. The parameter given
in the WHERE clause have a form;
Attribute_name θ value
Here, θ would mean any valid comparison operators (=, <, >, <=, >=, <>). Then, the
query would be written as follows;
SELECT * FROM Employee WHERE department = ‘CSE’;
Result:
Emp_I Emp_Name DoB Department Designation DoJ Salary
D
F115 Raguvaran 10-AUG- CSE Asst. Prof. 05-MAY- 27000
1982 2007
F114 Jennifer 10-SEP-1975 CSE Asst. Prof. 03-JUN- 35000
2004

3. Get the details about the employees who have joined after ’10-JUN-2005’.
The condition given in the question is Date of Joining. The query is,
SELECT * FROM Employee WHERE DoJ > ’10-JUN-2005’;
Result:
Emp_I Emp_Name DoB Department Designation DoJ Salary
D
F111 Kumar 25-MAY- Mechanical Asst. Prof. 02-MAY- 30000
1980 2006
F115 Raguvaran 10-AUG- CSE Asst. Prof. 05-MAY- 27000
1982 2007
4. Find all the employees who earn more than 30000.
The condition is about Salary attribute.
SELECT * FROM Employee WHERE Salary > 30000;
Result:
Emp_ID Emp_Name DoB Department Designation DoJ Salary
F110 Sam 15-JUN- Bio-Technology Professor 12-APR-2001 45000
1970
F114 Jennifer 10-SEP-1975 CSE Asst. Prof. 03-JUN- 35000
2004
F117 Ismail 15-MAY- IT Asst. Prof. 10-MAY- 33000
1979 2005

5. Get the details of employees who are not ‘Professor’.


The condition involves the attribute Designation.
SELECT * FROM Employee WHERE Designation <> ‘Professor’;
Result:
Emp_I Emp_Name DoB Department Designation DoJ Salary
D
F111 Kumar 25-MAY- Mechanical Asst. Prof. 02-MAY- 30000
1980 2006
F115 Raguvaran 10-AUG- CSE Asst. Prof. 05-MAY- 27000
1982 2007
F114 Jennifer 10-SEP-1975 CSE Asst. Prof. 03-JUN- 35000
2004
F117 Ismail 15-MAY- IT Asst. Prof. 10-MAY- 33000
1979 2005

6. Find the name, date of birth, and designation of all the employees who work
for ‘IT’ department.
Question 6 is different from all the above 5. It needs different parameters other
than *. That is, we need to display only what columns (attributes required). We
need to display name, DoB, and Designation of all employee records which satisfy
the condition “Department = ‘IT’”.
SELECT Emp_Name, DoB, Designation FROM Employee WHERE Department =
‘IT’;
Here, the parameters for the SELECT clause are separated using commas (‘,’).
Result:
Emp_Name DoB Designation
Ismail 15-MAY-1979 Asst. Prof.

7. Find all the departments which are offering salary above 25000.
For this question we need to display Departments. The condition to be satisfied is
Salary > 30000.
SELECT Department FROM Employee WHERE Salary > 25000;
Result:

Department
Bio-Technology
Mechanical
CSE
CSE
IT

8. Get the DoB of employee named ‘Kumar’.


We need Date of Birth. The condition involves Emp_Name attribute.
SELECT DoB FROM Employee WHERE Emp_Name = ‘Kumar’;
Note: The parameters like the list of attribute names, the list of table names, and
even the keywords are CASE INSENSITIVE. But, the literal values specified between
the set of quotes are CASE SENSITIVE. Hence, the above query would return a
result only if you have a record with the Emp_Name value as ‘Kumar’. If you have
‘kumar’, it cannot include that record in the result.
Result:
DoB

25-MAY-1980

9. Find the names and departments of employees who earn the salary in the range
20000 to 40000.
We need to display the columns Emp_Names, and Departments. The condition is a
range of values (salary). It can be written in two ways at least;
(a) SELECT Emp_Name, Department FROM Employee WHERE Salary >= 20000
AND Salary <= 40000;
(b) SELECT Emp_Name, Department FROM Employee WHERE Salary
BETWEEN 20000 AND 40000;
Here, you can find two new things. The first one is how do we mention two or
more conditions in the WHERE clause using AND logical connective. The second is
the keyword BETWEEN, which is used to mention a range of values. The simple
syntax for that would be as follows;

WHERE attribute BETWEEN value1 AND value 2.
Result:

Emp_Name Department
Kumar Mechanical
Raguvaran CSE
Jennifer CSE
Ismail IT
10. Find the employee details of any employee who work for ‘CSE’ and earn more
than 30000.
This question requires all the records from Employee table. The conditions are the
Department and Salary.
SELECT * FROM Employee WHERE Department = ‘CSE’ AND Salary >
30000;
Result:
Emp_ID Emp_Name DoB Department Designation DoJ Salary
F114 Jennifer 10-SEP-1975 CSE Asst. Prof. 03-JUN- 35000
2004
Simple SQL Exercises with Answers / SQL Exercises for simple Table
creation and SELECT queries / Queries involving selection, projection,
joins, and order by clauses
Solve and write necessary Queries for the case given below;
Consider the relation schemas of a selling database given below. Primary keys of
all the tables are underlined.

PART(Part_Number, Part_Description, Quantity_On_Hand, Class, Warehouse,


Price)
ORDERLINE(Order_Number, Part_Number_Ordered, Quantity_Ordered,
Quoted_Price)
ORDERS(Order_Number, Order_Date, Customer_Number)
CUSTOMER(Customer_Number, Last_Name, First_Name, Street, City, State,
Pincode, Balance, Credit_Limit, Sales_Representative_Number)
SALESREP(Sales_Representative_Number, Last_Name, First_Name, Street,
City, State, Pincode, Commission, Rate)

Write SQL Queries to achieve the following;


1. Display the Part_Number, Part_Description, Quantity_On_Hand, and Price of all the
parts in ascending order of Part_Description.
2. List down the Pincode, Last_name, Street, City, and State of every customer in the
ascending order of the Pincode.
3. Produce a list showing Part_Number, Part_Description, Quantity_On_Hand, and
Price sorted by both Warehouse and Class.
4. List all the records of ORDERLINE by selecting only rows where the
Quantity_Ordered is greater than or equal to 2.
5. List all customers’ Last_Name and First_Name whose Credit_Limit is less than or
equal to 10000.
6. List the Last_Name and First_Name of customers whose Credit_Limit is greater
than or equal to 10000 and Pincode is 649219.
7. Display all the parts that have a Part_Number that begins with ‘B’
8. Find the part number, part description, number of parts ordered and the quoted
price for every part that are ordered.
9. List the Part_Number, Part_Description, Quantity_Ordered and Quoted_Price of all
the parts whose Part_Number begins with ‘C’
10. Find the order number, date of order, of every order made by customer along with
the part numbers, part description, number of parts ordered and the quoted price
of the part.
11. List the Order_Number, Order_Date, Part_Number, Part_Description,
Quantity_Ordered and Quoted_Price of all the orders where the minimum quantity
ordered is at least 10.
12.Find the Customer’s Last_Name and First_Name followed by the Order_Number,
Part_Description and Quantity_Ordered for all of his orders.
Answers:
In all the queries, the words mentioned in ALL CAPS (except in tables) are
keywords used in SQL.

1. Display the Part_Number, Part_Description, Quantity_On_Hand, and Price of all


the parts in ascending order of Part_Description.
What we need to display? (SELECT clause) Part_Number, Part_Description,
Quantity_On_Hand, and Price
Where do we get it? (Table/Tables) Part table
(FROM Clause)
Conditions (if any) No Conditions
(WHERE clause)
Special Information Order the tuples on Part_Description in ascending
order
(Any other clauses)

Query:
SELECT Part_Number, Part_Description, Quantity_On_Hand, Price
FROM Part
ORDER BY Part_Description;

2. List down the Pincode, Last_name, Street, City, and State of every customer in
the ascending order of the Pincode.
What we need to display? (SELECT clause) Pincode, Last_name, Street, City, and State of
every customer
Where do we get it? (Table/Tables) (FROM Customer table
Clause)
Conditions (if any) No conditions
(WHERE clause)
Special Information Order the records on Pincode attribute in
ascending order
(Any other clauses)

Query:
SELECT Pincode, Last_Name, Street, City, State
FROM Customer
ORDER BY Pincode;
3. Produce a list showing Part_Number, Part_Description, Quantity_On_Hand, and
Price sorted by both Warehouse and Class.
What we need to display? Part_Number,
(SELECT clause) Part_Description, Quantity_On_Hand, and
Price of parts
Where do we get Part table
it? (Table/Tables) (FROM
Clause)
Conditions (if any) No Conditions
(WHERE clause)
Special Information Order by both Warehouse and Class
attributes in ascending order
(Any other clauses)

Query:
SELECT Part_Number, Part_Description, Quantity_On_Hand, Price
FROM Part
ORDER BY Warehouse, Class;
(Note: This query will order all the records on Warehouse attribute first and if there are more records with same
Warehouse values will be orders based on the Class value in ascending order)

4. List all the records of ORDERLINE by selecting only rows where the
Quantity_Ordered is greater than or equal to 2.
What we need to display? (SELECT Whole table, i.e., all the columns. Hence,
clause) we would use * to mention all the
attributes.
Where do we get Orderline table
it? (Table/Tables) (FROM Clause)
Conditions (if any) The ordered part’s Part Number should be
greater than or equal to zero, i.e,
(WHERE clause)
Part_Number_Ordered >=2
Special Information Nil
(Any other clauses)

Query:
SELECT *
FROM Orederline
WHERE Part_Number_Ordered >= 2;
5. List all customers’ Last_Name and First_Name whose Credit_Limit is less than or
equal to 10000.
What we need to display? (SELECT Last name and first name of the
clause) customers
Where do we get it? (Table/Tables) Customer table
(FROM Clause)
Conditions (if any) Value of credit_limit is <=10000
(WHERE clause)
Special Information Nil
(Any other clauses)

Query:

SELECT Last_Name, First_Name


FROM Customer
WHERE Credit_Limit <= 10000;
6. List the Last_Name and First_Name of customers whose Credit_Limit is greater
than or equal to 10000 and Pincode is 649219.
What we need to display? (SELECT Last name and first name of the
clause) customers
Where do we get it? (Table/Tables) Customer table
(FROM Clause)
Conditions (if any) Value of credit_limit is >=10000
(WHERE clause) Pincode should be 649219
Special Information Nil
(Any other clauses)

Query:
SELECT Last_Name, First_Name
FROM Customer
WHERE Credit_Limit >= 10000 AND Pincode = 649219;

7. Display all the parts that have a Part_Number that begins with ‘B’
What we need to display? (SELECT * / All the columns
clause)
Where do we get it? (Table/Tables) Parts table
(FROM Clause)
Conditions (if any) Part numbers that start with B. For
example, ‘B101’
(WHERE clause)

Special Information Nil


(Any other clauses)

Query:
SELECT *
FROM Parts
WHERE Part_Number LIKE ‘B%’;
(Note: in the question, the given condition specifies a portion of the value. That
means, we need to match a substring of the actual values. Hence, we have to use
the keyword LIKE. LIKE matches the substring given to the right of it with the
values stored in the column which is to the left of LIKE)

8. Find the part number, part description, number of parts


ordered(Quantity_Ordered) and the quoted price for every part that are ordered.
What we need to display? (SELECT Part number, part description, quantity
clause) ordered and quoted price
Where do we get 1. Part_Number and Part_Description are
it? (Table/Tables) (FROM available in Table PART.
Clause)
2. Quantity_Ordered and Quoted_Price are
part of ORDERLINE table.
Part_Number_Ordered attribute (foreign
key) of ORDERLINE table references the
value of Part_Number attribute of PART
table.
Two tables are involved – PART and
ORDERLINE
Conditions (if any) Only join condition (in case of CARTESIAN
Product). That is, the values of common
(WHERE clause)
attributes of both tables should be
matched.
Special Information Nil
(Any other clauses)

Query:
SELECT Part_Number, Part_Description, Quantity_Ordered, Quoted_Price
FROM Part, Orderline
WHERE Part.Part_Number = Orderline.Part_Number_Ordered;
(Note: The dot notation ‘Part.Part_Number’ is used because in some cases the
common attributes might be named with same attribute name. For the above
query, we can even mention the WHERE condition as Part_Number =
Part_Number_Ordered as these two are pointing same domain with different
names.)
9. List the Part_Number, Part_Description, Quantity_Ordered and Quoted_Price of
all the parts whose Part_Number begins with ‘C’
What we need to display? (SELECT Part number, part description, quantity
clause) ordered and quoted price
Where do we get 1. Part_Number and Part_Description are
it? (Table/Tables) (FROM available in Table PART.
Clause)
2. Quantity_Ordered and Quoted_Price are
part of ORDERLINE table.
Part_Number_Ordered attribute (foreign
key) of ORDERLINE table references the
value of Part_Number attribute of PART
table.
Two tables are involved – PART, and
ORDERLINE
Conditions (if any) First condition is join condition (CARTESIAN
Product) which matches the values of
(WHERE clause)
common attributes of both tables.
Second condition mentions only the part
numbers that begins with the alphabet ‘C’.
Special Information Nil
(Any other clauses)

Query:
SELECT Part_Number, Part_Description, Quantity_Ordered, Quoted_Price
FROM Part, Orderline
WHERE Part.Part_Number = Orderline.Part_Number_Ordered AND Part_Number
LIKE ‘C%’;
[Refer the note of Query 7]
10. Find the order number, date of order of every order made by customer along
with the part numbers, part description, number of parts ordered and the quoted
price of the part.
What we need to display? (SELECT Order number, Order date, Part number,
clause) Part description, Quantity ordered and
quoted price
Where do we get 1. Part_Number and Part_Description are
it? (Table/Tables) (FROM available in Table PART.
Clause)
2. Quantity_Ordered and Quoted_Price are
part of ORDERLINE table.
Part_Number_Ordered attribute (foreign
key) of ORDERLINE table references the
value of Part_Number attribute of PART
table.
3. Order_Number and Order_Date are part
of ORDER table.
Order_Number attribute (foreign key) of
ORDERLINE table refers Order_Number
attribute (Primary Key) of ORDER table.
Three tables are involved – PART,
ORDERLINE and ORDER
Conditions (if any) Only the join condition (CARTESIAN
Product) which matches the values of
(WHERE clause)
common attributes of all the three tables.
One condition matches the common
attributes of PART and ORDERLINE and the
second join condition matches the common
attributes of ORDERLINE and ORDER tables.
Special Information Nil
(Any other clauses)

Query:
SELECT Order.Order_Number, Order_Date, Part_Number, Part_Description,
Quantity_Ordered, Quoted_Price
FROM Order, Part, Orderline
WHERE Order.Order_Number = Orderline.Order_Number AND Part.Part_Number =
Orderline.Part_Number_Ordered;
(Note: Observe from the above query for dot notation Order.Order_Number. In
this case, we must use dot notation and specify which table we refer. Otherwise,
it will cause ambiguity. That is, if we use Order_Number without prefix the name
of the table, the query processor confused with the name as Order_Number
available in both tables ORDER and ORDERLINE)
11. List the Order_Number, Order_Date, Part_Number, Part_Description,
Quantity_Ordered and Quoted_Price of all the orders where the minimum quantity
ordered is at least 10.
What we need to display? (SELECT Order number, Order date, Part number,
clause) Part description, Quantity ordered and
quoted price
Where do we get 1. Part_Number and Part_Description are
it? (Table/Tables) (FROM available in Table PART.
Clause)
2. Quantity_Ordered and Quoted_Price are
part of ORDERLINE table.
Part_Number_Ordered attribute (foreign
key) of ORDERLINE table references the
value of Part_Number attribute of PART
table.
3. Order_Number and Order_Date are part
of ORDER table.
Order_Number attribute (foreign key) of
ORDERLINE table refers Order_Number
attribute (Primary Key) of ORDER table.
Three tables are involved – PART,
ORDERLINE and ORDER
Conditions (if any) One join condition matches the common
attributes of PART and ORDERLINE and the
(WHERE clause)
second join condition matches the common
attributes of ORDERLINE and ORDER tables.
The other condition verifies for the
quantity greater than or equal to 10.
THREE conditions are used.
Special Information Nil
(Any other clauses)

Query:
SELECT Order.Order_Number, Order_Date, Part_Number, Part_Description,
Quantity_Ordered, Quoted_Price
FROM Order, Part, Orderline
WHERE Order.Order_Number = Orderline.Order_Number AND Part.Part_Number =
Orderline.Part_Number_Ordered AND Quantity_Ordered >= 10;
12. Find the Customer’s Last_Name and First_Name followed by the
Order_Number, Part_Description and Quantity_Ordered for all of his orders.
What we need to display? (SELECT Last name and First name of the customers,
clause) Order number, Part description, and
Quantity ordered
Where do we get 1. Part_Description is available in Table
it? (Table/Tables) (FROM PART.
Clause)
2. Quantity_Ordered is part of ORDERLINE
table.
Part_Number_Ordered attribute (foreign
key) of ORDERLINE table references the
value of Part_Number attribute of PART
table.
3. Customer First name and Last name are
part of CUSTOMER table.
Customer_Number attribute (foreign key) of
ORDER table refers Customer_Number
attribute (Primary Key) of CUSTOMER table.
CUSTOMER and is linked to ORDERLINE
through ORDER table only.
Hence, four tables are involved – PART,
ORDERLINE ORDER, and CUSTOMER
Conditions (if any) One join condition matches the common
attributes of PART and ORDERLINE, the
(WHERE clause)
second join condition matches the common
attributes of ORDERLINE and ORDER tables,
third join condition matches CUSTOMER
with ORDER.
THREE conditions are used.
Special Information Nil
(Any other clauses)

Query:
SELECT Last_Name, First_Name, Order.Order_Number, Part_Description,
Quantity_Ordered
FROM Customer, Order, Orderline, Part
WHERE Customer.Customer_Number = Order.Customer_Number AND
Order.Order_Number = Orderline.Order_Number AND Part_Number =
Part_Number_Ordered;
(Note: Here, the condition Part_Number = Part_Number_Ordered is written
without prefixing the table names which is valid)

You might also like