Sei sulla pagina 1di 6

1.

What is the difference between a Facts Table (or a Base table) and a
Dimension Table? (1 mark)
ANSWER:
Fact table in a data warehouse consists of facts and/or measures. The nature of data in a fact table is
usually numerical.
On the other hand, dimension table in a data warehouse contains fields used to describe the data in fact
tables. A dimension table can provide additional and descriptive information (dimension) of the field of a fact
table.

2. What is the difference between JOIN and UNION? (1 mark)


ANSWER:

JOIN allows us to lookup records on other table based on the given


conditions between two tables.
UNION operation allows us to add 2 similar data sets to create resulting data
set that contains all the data from the source data sets. Union does not
require any condition for joining.
3. Choose the correct output of this query? (1 mark)
SELECT a.customer_id
FROM TableA as a
LEFT OUTER JOIN TableB as b
ON a.customer_id = b.customer_id
WHERE b.country_code = 'AUSTRALIA
a. TableA UNION (TableA INTERSECT TableB)
b. TableA INTERSECT TableB
c. TableA MINUS TableB
d. TableA UNION TableB
ANSWER: (b) TableA INTERSECT TableB

4. What is the difference between WHERE clause and HAVING clause? (1 mark)
ANSWER:

WHERE clause can only be applied on a static non-aggregated column


whereas we need to use HAVING for aggregated columns.
Unlike WHERE, HAVING can be used only with the SELECT statement.
5. What
a.
b.
c.
d.

type of the SQL code is the clause SET table? (1 mark)


DDL
DML
DCL
DQL

ANSWER: (a) DCL

6. What is a Self Join? Mention one use of Self Join using an example. (1+2=3
marks)
ANSWER:

A self join is a join in which a table is joined with itself (which is also called Unary relationships), specially
when the table has a FOREIGN KEY which references its own PRIMARY KEY.
Example:
SELECT e.name EMPLOYEE, m.name MANAGER
FROM EMPLOYEE e, EMPLOYEE m
WHERE e.mgr_id = m.id (+)

7. What are the differences among ROWNUM, RANK and DENSE_RANK? (2


marks)
ANSWER:

ROW_NUMBER assigns contiguous, unique numbers from 1.. N to a result set.


RANK does not assign unique numbersnor does it assign contiguous
numbers. If two records tie for second place, no record will be assigned the
3rd rank as no one came in third, according to RANK.
DENSE_RANK, like RANK, does not assign unique numbers, but it does assign
contiguous numbers. Even though two records tied for second place, there is
a third-place record.
8. What is difference between DELETE, TRUNCATE and DROP commands? (2
marks)
ANSWER:
DELETE command deletes only the rows from the table based on the condition given in the WHERE clause
or deletes all the rows from the table if no condition is specified. But it does not free the space containing the
table. Can be rolled back.
TRUNCATE command is used to delete all the rows from the table and free the space containing the table.
Cannot be rolled back.
DROP statement will delete or purge the table and free the space containing the table. Cannot be rolled
back.

9. What's the difference between a primary key and a unique key? (1 mark)
ANSWER: Primary key doesn't allow NULLs, but unique key allows one NULL
only.
10.Values in Primary key can be updated or Modified. (True/False)
Explain your answer. (1 mark)
ANSWER:
False
The value of the primary key in the table cannot be updated due to the Parent child relationships, as the
primary key in one table (parent table) would a foreign key in another table(child table).
Unless and until the values in the child table are deleted, the values in the parent tables cannot be updated
or modified.

11.How many NULL values can a unique key allow? (1 mark)


a. One
b. None
c. Many
d. Depends on table structure
ANSWER: (a) One
12.What is a FOREIGN KEY? (1 mark)

ANSWER:
A FOREIGN KEY is a column or a group of columns whose values are derived
from the Primary key or the unique key of some other table. Foreign keys
represent relationships between tables, i.e., foreign key constraints are used
to enforce referential integrity.
13.What is a Composite Primary Key? (1 mark)
ANSWER:
Composite primary key is a primary key, which is made up of two or more
fields (columns).
14.What is the difference between IN and EXISTS? (2 marks)
ANSWER:
The IN operator returns a row if a WHERE conditioned table.field value
matches a list of IN values. IN is typically used as part of a main query or in
conjunction with a subquery.
The EXISTS operator returns all main query rows if the subquery contains any
rows. EXISTS is only used in conjunction with a subquery.
Performance wise, EXISTS works faster than IN
15.What is a view? (1 mark)
ANSWER:
A view is a virtual table based on the result-set of an SQL statement.
16.Which of the below operations are valid for VIEWS? (1 mark)
a. INSERT
b. UPDATE
c. DELETE
d. DROP
ANSWER: a,b,c,d
All operations DROP is valid for both read-only and updatable views.
INSERT, DELETE and UPDATE are valid only on updatable views defined from a single table

17.What is a Cartesian product? (1 mark)


ANSWER:
A Cross join results in a Cartesian product. This means every row of the left
table is combined with every row of the right table.
18.Name the different types of Joins? (1 mark)
ANSWER:
5 joins: Inner, Left Outer, Right Outer, Full Outer, Cross

19.What will be the result of the below query? Explain your answer. (1 mark)
SELECT CASE WHEN NULL = NULL THEN 'Yes' ELSE 'No' END AS RESULT
ANSWER:
Result will always be No. The correct syntax for operation on NULL values is
IS NULL
20.What will be the result of the below query? (1 mark)
SELECT max(Emp_Salary) FROM Emp_Detail WHERE Emp_Salary NOT
IN(SELECT max(Emp_Salary) FROM Emp_ Detail)
ANSWER: Second highest salary
21.What is the output of below query for the below tableA? (1 mark)
custome transactio transaction_ amount
r_id
n_id
date
(USD)
101 a001
1/1/2014
5
102 a002
2/2/2014
10
101 a003
4/2/2014
4
103 a004
8/5/2014
10
104 a005
10/7/2014
100
105 a006
11/8/2014
150
101 a007
12/8/2014
20
102 a008
12/12/2014
14
SELECT Customer_id
FROM TableA
WHERE SUM(amount) >50
GROUP BY Customer_id
ANSWER: Error as aggregate function cannot be used in the WHERE clause

22.Name 2 missing syntactical components in the below code? (2 marks)


SELECT
a.customer_id
,a.month_name
,sum(a.transaction_amount)
FROM <Transaction_Table>
WHERE
a.year_id = 2015
ORDER BY a.month_name
ANSWER:
1. Alias name in front of table <Transaction_Table>
2. GROUP BY function.

23.Write a single SELECT statement to select all the customers who have done
more than 25 USD of total transaction in the year 2014. (3 marks)
custome

transactio

transaction_

amount

r_id
101
102
101
103
104
105
101
102
103
106
107
108
109
110

n_id
a001
a002
a003
a004
a005
a006
a007
a008
a009
a010
a011
a012
a013
a014

date

(USD)

1/1/2013
2/2/2014
4/2/2014
8/5/2014
10/7/2014
11/8/2012
12/8/2014
12/12/2014
12/12/2014
14/12/2014
6/9/2013
7/10/2014
10/10/2014
12/11/2014

5
10
4
10
100
150
20
14
30
10
25
15
45
30

ANSWER:
SELECT customer_id
FROM <Transaction_table>
WHERE transaction_date BETWEEN 01/01/2014 AND 31/12/2014
GROUP BY 1 HAVING SUM(amount)>=25.00

24.What is the Syntax for the CASE statement? (1 mark)


ANSWER:
CASE WHEN <condition> THEN <value>
WHEN <condition> THEN <value>
WHEN <condition> THEN <value>
ELSE <value> END AS alias_name

25.What is the output of the below SQL code on the below table? (1 mark)
TableA
Student_
ID
1
2
3
4

Name
John
Mary
Mohan
Kiran

Score
86.2
78.5
68.9
82.9

SELECT
Name,
CAST(Score AS char(3)) Char_Score
From TableA
ANSWER:

Name
John

Char_Score
86.

Mary

78.

Mohan

68.

Kiran

82.

Potrebbero piacerti anche