Oracle SQL Flashcards

1
Q

What is SQL?

A

Structured Query Language (SQL) is a query language used for designing, processing and querying the data stored in RDBMS.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

AUTHORS: AUTHOR_NAME, BOOK_NAME

BOOKS: BOOK_NAME, COPIES_SOLD

Write a query to show top 3 authors who sold most books in total.

A
  1. First we need to sum the COPIES_SOLD per author

SELECT A.AUTHOR_NAME, B.COPIES_SOLD
FROM AUTHOR A NATURAL JOIN BOOKS B
GROUP BY A.AUTHOR_NAME

  1. For maximum copies sold, we need to order it in descending order

ORDER BY 2 DESC

  1. And to see top 3, we need to fetch first 3 rows only
SELECT A.AUTHOR_NAME, B.COPIES_SOLD
FROM AUTHOR A NATURAL JOIN BOOKS B
GROUP BY A.AUTHOR_NAME
ORDER BY 2 DESC
FETCH FIRST 3 ROWS ONLY;
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

EMP: DEPT_NAME, EMP_ID, EMP_NAME

SAL: SALARY, EMP_ID, EMP_NAME

Write a query to print every department where average salary per employee is lower than $500.

A

SELECT E.DEPT_NAME, S.AVG(SALARY)
FROM EMP E NATURAL JOIN SAL S
GROUP BY E.DEPT_NAME
HAVING S.AVG(SALARY)<500;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

EMP: EMP_ID, EMP_NAME, SAL

Write a query to find the 5th maximum salary.

A
  1. First arrange the table to show distinct salaries in descending order (because we want maximum)

SELECT DISTINCT SAL
FROM EMP
ORDER BY 1 DESC

  1. We need 5th maximum, so lets fetch only 5 rows

SELECT DISTINCT SAL
FROM EMP
ORDER BY 1 DESC
FETCH FIRST 5 ROWS ONLY

  1. Now if we take the minimum salary from resultset, we would get 5th maximum:
SELECT MIN(SAL) FROM 
( 
SELECT DISTINCT SAL
FROM EMP
ORDER BY 1 DESC
FETCH FIRST 5 ROWS ONLY
);
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

EMP: EMP_ID, EMP_NAME, SAL

Write a query to find the 3rd minimum salary.

A
  1. First arrange the table to show distinct salaries in ascending order (because we want minimum)

SELECT DISTINCT SAL
FROM EMP
ORDER BY 1

  1. We need 3rd minimum, so lets fetch only 3 rows

SELECT DISTINCT SAL
FROM EMP
ORDER BY 1
FETCH FIRST 3 ROWS ONLY

  1. Now if we take the maximum salary from resultset, we would get 3rd minimum:
SELECT MAX(SAL) FROM 
( 
SELECT DISTINCT SAL
FROM EMP
ORDER BY 1 
FETCH FIRST 3 ROWS ONLY
);
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

EMP: EMP_ID, EMP_NAME, ADDRESS, SAL, DEPTNO, HIRED_DATE, JOB

Write a query to find out number of columns in this table.

A
SELECT COUNT(COLUMN_NAME)
FROM USER_TAB_COLUMNS
WHERE TABLE_NAME = 'EMP';
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Why doesn’t the aliasing work this way:

SELECT SAL AS SALARY
FROM EMP
WHERE SALARY<1000

but works this way:

SELECT * FROM 
(
SELECT SAL AS SALARY
FROM EMP
) SUBQ
WHERE SALARY<1000
A

WHERE clause is evaluated before SELECT clause hence SALARY does not exist when WHERE clause is processed.

FROM clause is evaluated before WHERE and by placing the original query in a FROM clause, we make sure that results are generated for outermost WHERE clause and it sees the alias.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How to limit the number of rows returned (nor concerned with order, any n rows will do)?

A

SELECT * FROM EMP
FETCH FIRST 5 ROWS ONLY;

SELECT * FROM EMP
WHERE ROWNUM<=5;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Why does this query work:

SELECT * FROM EMP
WHERE ROWNUM <= 5;

but not this one:

SELECT * FROM EMP
WHERE ROWNUM = 5;

A

When Oracle executes the query, it forms a resultset.

Then it fetches the first row, calls it row number 1 and checks if it has gotten past row number 5 yet. If not, it returns the row, as it matches with the condition “less than or equal to 5”. It fetches next row and increments its counter.

This shows that ROWNUM is assigned after each row is fetched.

Using an equality condition with ROWNUM does not work the same way, Oracle fetches a row from its resultset, calls it row number 1 and checks if it has gotten past row number 5. It is not, but it also doesn’t meet the criteria “equal to 5” and hence Oracle discards the row. It then fetches next row and calls it row number 1. This goes on until the resultset is exhuasted.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

How to write below query such that it gives 5 random rows every time it is executed?

SELECT * FROM EMP
FETCH FIRST 5 ROWS ONLY;

A

With the help of built-in function DBMS_RANDOM.VALUE()

SELECT * FROM EMP
ORDER BY DBMS_RANDOM.VALUE()
FETCH FIRST 5 ROWS ONLY;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Why can’t we use below query to find if COMM is null?

SELECT * FROM EMP
WHERE COMM=NULL;

A

NULL is not equal to anything, not even itself and hence “=” or “!=” cannot be used to test if a column is null

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Explain NVL.

A

NVL() replaces null value of given column with value given by user.

For eg. NVL( Commission, 0)

If Commission is null for a particular row, it will substitute it with 0 for the resultset. If it is not null, then the original value will be displayed.

NVL takes only two arguments and will evalute the second expression even when first argument is not null. This is time consuming and hence inefficient.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Explain COALESCE.

A

COALESCE() function accepts a list of arguments and returns the first one that evaluates to a non-null value.

For eg. COALESCE (Name, LAST_NAME, FULL_NAME, ‘NO NAME’)

If Name column is null, Oracle will check LAST_NAME column and put its value if it is not null. If LAST_NAME is null, then it will check FULL_NAME column and stop only when it comes across first non-null value.

In the event of all arguments being null, COALESCE will return null. COALESCE uses short-circuit evaluation meaning that the function stops evaluating the remaining expressions once it finds the first one evaluates to a non-null value.

COALESCE requires that all arguments be of same datatype and hence
COALESCE (Name, 1, ‘A’) will result in error.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What will be the output:

SELECT
COALESCE (1+1, 1/0)
FROM DUAL;

A

2

COALESCE will evaluate the first expression, see that its not null and will not evaluate second expression.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

How to match any number to character and just one character?

A

Any number of characters: %

Just one character: _ (underscore)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Explain TRANSLATE.

A

TRANSLATE() replaces a sequence of characters in a string with another set of characters. However it replaces single character at a time.

For eg. TRANSLATE(‘1tech23’, ‘123’, ‘456’) = ‘4tech56’

TRANSLATE(‘222tech’, ‘2ec’, ‘3it’) = 333tith

17
Q

Explain REPLACE.

A

REPLACE() replaces a sequence of characters in a string with another set of characters.

For eg. REPLACE(‘123123tech’, ‘123’) = ‘tech’

REPLACE(‘123tech123’, ‘123’) = ‘tech’

REPLACE(‘222tech’, ‘2’, ‘3’) = ‘333tech’

REPLACE(‘0000123’, ‘0’) : ‘123’

18
Q

Whats the difference between UNION and UNION ALL?

A

UNION will keep distinct records only.

UNION ALL will keep all the records.

19
Q

How to create a new table similar to an old table without data?

A

CREATE TABLE NEW_TABLE
AS
SELECT * FROM OLD_TABLE
WHERE 1=2;

The WHERE condition will always be false and no data will be copied, only structure will be copied.

20
Q

What is PL/SQL?

A

Procedural Language extension to SQL (PL/SQL) extends SQL by adding programming structures and subroutines available in any high-level language. It can be used both on server side and client side.

For eg. triggers (server side)