SQL 2
SQL 2
Basics of sql
DDL statements
DML statements
Aggregate functions
SQL clauses
Joins
Cursor, Trigger and indexing
PL\SQL
Outcomes
Understand the basics of sql.
Apply the concepts of sql to solve various
queries..
Learn the basic of PL\SQL.
Apply cursor, trigger and indexes in database.
Apply the concepts of procedural language
concepts to create procedure, function etc.
Types of Questions asked from SQL
1. What is DDL and DML. Name some command?
2. How char is different from varchar?
3. Tell me the difference between float and double.
4. What is BLOB and CLOB with their uses?
5. How we can find thee minimum salary from any relation?
6. How we can find four maximum salary?
7. How to find max and min salary from a relation?
8. What is the need of trigger.?
9. What is the difference between function and stored procedure?
10. How many max tables allows in join operation?
11. How many max columns are possible in any relation?
12. How many max rows\ tuples are possible in any relation?
13. What is the need of cursor and how many types of cursor and their uses?
14. What is indexing and how to apply indexing in a database?
15. Questions based on aggregate functions.
16. Question based on group by and ordered by.
17. How to find distinct value?
18. How to find duplicate tuple?
19. How many triggers allowed in mysql table?
20. How many rows will return from dual table.?
21. Write an SQL query to find names of employee start with ‘A’?
22. Write a SQL query to make a column as unique?
23. How many tables can be joined in SQL Server?
24. How many columns can exist together per table?
25. What is SQL Profiler?
26. Explain what is meant by replication of database?
27. What are Denormalization and Normalization?
28. Differentiate between DROP, DELETE, And TRUNCATE Command.
29. What is the difference between SQL, MySQL, and PL/SQL?
30. What Clauses are used in SQL?
31. Write an SQL query to fetch “FIRST_NAME” from Worker table in upper case.
32. Write an SQL query to print the first three characters of FIRST_NAME from Worker
table.
33. How many triggers allowed in mysql table?
34. SQL Query to find Max Salary from each department.
35. SQL Query to find second highest salary of Employee.
SQL
DDL( Data Definition Language)
• CREATE – is used to create the database or its objects (like table, index, function,
views, store procedure and triggers).
• DROP – is used to delete objects from the database.
• ALTER-is used to alter the structure of the database.
• TRUNCATE–is used to remove all records from a table, including all spaces
allocated for the records are removed.
• COMMENT –is used to add comments to the data dictionary.
• RENAME –is used to rename an object existing in the database.
• sum(salary): Sum all Non Null values of Column salary i.e., 310
sum(Distinct salary): Sum of all distinct Non-Null values i.e., 250.
Avg():
Min():
• Min(salary): Minimum value in the salary column except NULL i.e., 40.
Max():
Marks
SQL Exercises based on Multiple Tables
1. Write a query to find those customers with their name and those
salesmen with their name and city that lives in the same city.
2. Write a SQL statement to find the names of all customers along with the
salesmen who works for them.
3. Write a SQL statement to display all those orders by the customers not
located in the same cities where their salesmen live.
4. Write a SQL statement that finds out each order number followed by
the name of the customers who made the order.
5. Write a SQL statement that sorts out the customer and their grade who
made an order. Each of the customers must have a grade and served by
at least a salesman, who belongs to a city.
6. Write a query that produces all customers with their name, city,
salesman and commission, who served by a salesman and the salesman
works at a rate of the commission within 12% to 14%.
7. Write a SQL statement that produces all orders with the order number,
customer name, commission rate and earned commission amount for
those customers who carry their grade is 200 or more and served by an
existing salesman.
SQL Exercises based on SUBQUERIES
1. Write a query to display all the orders from the orders table issued by the
salesman 'Paul Adam'.
2. Write a query to display all the orders for the salesman who belongs to the
city London.
3. Write a query to find all the orders issued against the salesman who may
works for customer whose id is 3007.
4. Write a query to display all the orders which values are greater than the
average order value for 10th October 2012.
5. Write a query to find all orders attributed to a salesman in New york.
6. Write a query to display the commission of all the salesmen servicing
customers in Paris.
7. Write a query to display all the customers whose id is 2001 bellow the
salesman ID of Mc Lyon.
8. Write a query to count the customers with grades above New York's average.
9. Write a query to display all customers with orders on October 5, 2012.
10. Write a query to display all the customers with orders issued on date 17th
August, 2012.
SQL Exercises - Aggregate Functions and Group by statements
1. Write a SQL statement to find the total purchase amount of all orders.
2. Write a SQL statement to find the average purchase amount of all orders.
3. Write a SQL statement to find the number of salesmen currently listing for all of
their customers.
4. Write a SQL statement know how many customer have listed their names.
5. Write a SQL statement find the number of customers who gets at least a gradation
for his/her performance.
6. Write a SQL statement to get the maximum purchase amount of all the orders.
7. Write a SQL statement to get the minimum purchase amount of all the orders.
8. Write a SQL statement which selects the highest grade for each of the cities of the
customers.
9. Write a SQL statement to find the highest purchase amount with their ID, for only
those customers whose ID is within the range 3002 and 3007.
10. Write a SQL statement that counts all orders for a date August 17th, 2012.
11. Write a query that counts the number of salesmen with their order date and ID
registering orders for each day.
12. Write a SQL query to calculate the average price of all the products.
13. Write a SQL statement to find the highest purchase amount on a date '2012-08-17'
for each salesman with their ID.
SQL Exercises-JOINS
1. Write a SQL statement to prepare a list with salesman name, customer name and
their cities for the salesmen and customer who belongs to the same city.
2. Write a SQL statement to make a list with order no, purchase amount, customer
name and their cities for those orders which order amount between 500 and
2000.
3. Write a SQL statement to know which salesman is working for which customer.
4. Write a SQL statement to find the list of customers who appointed a salesman
for their jobs who gets a commission from the company is more than 12%.
5. Write a SQL statement to find the list of customers who appointed a salesman
for their jobs who does not live in the same city where their customer lives, and
gets a commission is above 12%
6. Write a SQL statement to find the details of a order i.e. order number, order
date, amount of order, which customer gives the order and which salesman
works for that customer and how much commission he gets for an order.
7. Write a SQL statement to make a list in ascending order for the customer who
works either through a salesman or by own.
8. Write a SQL statement to make a list in ascending order for the salesmen who
works either for one or more customer or not yet join under any of the
customers.
TRIGGERS
TRIGGERS are stored programs that are fired by Oracle engine automatically
when DML Statements like insert, update, delete are executed on the table or
some events occur. The code to be executed in case of a trigger can be defined
as per the requirement. You can choose the event upon which the trigger needs
to be fired and the timing of the execution. The purpose of trigger is to maintain
the integrity of information on the database.
Benefits of Triggers
• Generating some derived column values automatically
• Enforcing referential integrity
• Event logging and storing information on table access
• Auditing
• Synchronous replication of tables
• Imposing security authorizations
• Preventing invalid transactions
Types of Triggers in Oracle
Triggers can be classified based on the following parameters.
Classification based on the timing
– BEFORE Trigger: It fires before the specified event has occurred.
– AFTER Trigger: It fires after the specified event has occurred.
– INSTEAD OF Trigger: A special type. You will learn more about the
further topics. (only for DML )
Classification based on the level
– STATEMENT level Trigger: It fires one time for the specified event
statement.
– ROW level Trigger: It fires for each record that got affected in the
specified event. (only for DML)
Classification based on the Event
– DML Trigger: It fires when the DML event is specified
(INSERT/UPDATE/DELETE)
– DDL Trigger: It fires when the DDL event is specified (CREATE/ALTER)
– DATABASE Trigger: It fires when the database event is specified
(LOGON/LOGOFF/STARTUP/SHUTDOWN)
So each trigger is the combination of above parameters.
How to Create Trigger
Cursor in SQL
Cursor is a Temporary Memory or Temporary Work Station. It is Allocated by
Database Server at the Time of Performing DML operations on Table by User.
Cursors are used to store Database Tables. There are 2 types of Cursors:
Implicit Cursors, and Explicit Cursors. These are explained as following below.
• Implicit Cursors:
Implicit Cursors are also known as Default Cursors of SQL SERVER. These
Cursors are allocated by SQL SERVER when the user performs DML
operations.
• Explicit Cursors :
Explicit Cursors are Created by Users whenever the user requires them.
Explicit Cursors are used for Fetching data from Table in Row-By-Row
Manner.
How to create Explicit Cursor:
Indexing in Database
Indexing is a procedure that returns your requested data faster from the defined
table. Without indexing, the SQL server has to scan the whole table for your
data.
By indexing, SQL server do the exact same thing when you searched for a
content in a book by checking the index page. In the same way table’s index
allows us to locate the exact data without scanning the whole table. There are
two types of indexing in SQL.
1. Clustered index
2. Non-clustered index
1.Clustered –
Clustered index is the type of indexing that established a physical sorting order of
rows. Suppose you have a table Student_info which contains ROLL_NO as a
primary key than Clustered index which is self created on that primary key will
sort the Student_info table as per ROLL_NO. Clustered index is like Dictionary, in
the dictionary sorting order is alphabetical there is no separate index page.
2. Non-clustered:
The Non-Clustered index is an index structure separate from the data stored in a
table that reorders one or more selected columns. The non-clustered index is
created to improve the performance of frequently used queries not covered by
clustered index. It’s like a textbook, the index page is created separately at the
beginning of that book.