Вы находитесь на странице: 1из 6

SQL Test 2011

Name:

Agency:

Date:

Q1. What is the difference between clustered and non-clustered index?

Using clustered index data can be ordered by index. There can be only one
clustered index on table. It stores the tree in database. Non clustered index
can be used for columns which have repeated values. Primary key index is
clustered index.
Q2. What is de-normalization?
De-normalization is process of adding redundant data. This is done to increase
the performance. Data from normalized tables is merged into one table to
make the retrieval faster. This is done in OLAP databases especially.

Q3. What is an execution plan? When would you use it?


Execution plans tells us how the query was executed. It is used identify the
performance problems of a query. We can know what are the indexes used by
query, type of scan and how joins are performed. This is extremely useful in
debugging performance problems and optimize the queries.

Q4. What does NULL mean?

NULL means nothing. The column does not have any value. NULL is not equal
to anything. So we check if a column is null by using is null or not equal by
is not null.

SQL Test 2011

SQL Test 2011


Test item #1: Return the First Name, Last Name, Product Name, and Sale Price for all
products sold in the month of October 2005.

select c.firstname, c.lastname, p.productname, s.saleprice from Customers c,


Sales s, Products p
where c.customerid = s.customerid and s.productid = p.productid and
s.saledate between '01-OCT-2005' and '31-OCT-2005'
Test item #2: Return the CustomerID, First Name, and Last Name of those individuals in
the Customer table who have made no Sales purchases.

select c.customerid, c.firstname, c.lastname from Customers c where


c.customerid not in (select distinct customerid from sales)

Test item #3: Return the First Name, Last Name, Sale Price, Recommended Sale Price,
and the difference between the Sale Price and Recommended Sale Price for all Sales. The
difference must be returned as a positive number.
select c.firstname, c.lastname, s.saleprice, p.recommendedprice as
recommededsaleprice, GREATEST((s.saleprice - p.recommendedprice), 0) as
difference
from customers c, sales s, products p
where c.customerid = s.customerid and s.productid = p.productid

SQL Test 2011


Test item #4: Return the average Sale Price by Product Category.

select p.category, avg(s.saleprice) from products p, sales s where p.productid


= s.productid group by p.category

Test item #5: Add the following Customer and Sale information to the database.
FirstName: Chris
LastName: Kringle
City: Henryville
State: IN
Zip: 47126
ProductID: 3
SalePrice: 205
SaleDate: 12/31/2005

If customerid is auto incremented.


insert into customers(firstname, lastname, city, state, zip) values ('Chris',
'Kringle', 'Henryville', 'IN', '47126');
Otherwise
insert into customers(customerid, firstname, lastname, city, state, zip) values
((select max(customerid) + 1 from customers),'Chris', 'Kringle', 'Henryville',
'IN', '47126');
insert into sales(productid, customerid, saleprice, saledate) values (3, (select
max(customerid) from customers), 205, '31-DEC-2005');
commit;

Test item #6: Delete the customer(s) from the database who are from the state of
Maine ('ME').
4

SQL Test 2011

delete from customers where state = 'ME';


commit;

SQL Test 2011


Test item #7: Return the Product Category and the average Sale Price for those
customers who have purchased two or more products.

select p.category, avg(s.saleprice) from products p, sales s


where p.productid = s.productid and s.customerid in
(
select c.customerid, count(*) from customers c, sales s
where c.customerid = s.customerid
group by customerid
having count(*) >= 2
)
group by p.category

Test item #8: Update the Sale Price to the Recommended Sale Price of those Sales
occurring between 6/10/2005 and 6/20/2005.
update sales s
set s.saleprice = (select recommendedprice from products p where p.productid =
s.productid)
where s.saledate between '10-JUN-2005' and '20-JUN-2005';
commit;

Вам также может понравиться