SQL Fundamentals Flashcards

Basics of SQL

1
Q

How do you concat column names in different database systems?

A

DB2, Oracle, PostgreSQL - These databases use the double vertical bar as the concatenation operator:
~~~
select ename||’ WORKS AS A ‘||job as msg
from emp
where deptno=10
~~~
MySQL - This database supports a function called CONCAT:
~~~
select concat(ename, ‘ WORKS AS A ‘,job) as msg
from emp
where deptno=10
~~~

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

How do you use conditional logic in the SELECT statement?

A

Use the CASE expression to perform conditional logic directly in your SELECT
statement:

select ename,sal,
case when sal <= 2000 then 'UNDERPAID'
when sal >= 4000 then 'OVERPAID'
else 'OK'
end as status
from emp
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How do you limit the number of rows returned from a query in different databse systems?

A

MySQL and PostgreSQL - Do the same thing in MySQL and PostgreSQL using LIMIT:
~~~
select *
from emp limit 5
~~~
Oracle - In Oracle, place a restriction on the number of rows returned by restricting ROW‐
NUM in the WHERE clause:
~~~
select *
from emp
where rownum <= 5
~~~

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

How do you transform NULL values into some real value?

A

Use the function COALESCE to substitute real values for nulls:
~~~
select coalesce(comm,0)
from emp
~~~

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

How do you sort the result of a query in ascending order?

A

Use the ORDER BY clause:
~~~
select ename,job,sal
from emp
where deptno = 10
order by sal asc
~~~

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

How do you sort the result of a query based on a substring?

A

DB2, MySQL, Oracle, and PostgreSQL - Use the SUBSTRING function in the ORDER BY clause:
~~~
select ename,job
from emp
order by substring(job,start position, how many chars);
~~~
Note: SQL strings index starts from 1.

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

What does the ROW_NUMBER() window function do?

A

Assigns a unique identifier to each row by partition.
~~~
SELECT
ROW_NUMBER() OVER (ORDER BY column_name) AS row_num,
column1, column2
FROM your_table;
~~~

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

What does RANK() and DENSE_RANK() window function do? What are the difference between them?

A

Assigns a unique rank to each value in the partition with the same values receiving the same rank. With RANK() if two values are tied, at rank 2, then the next rank will be 4 (gaps in ranks), but DENSE_RANK() does not have gaps even with ties.
~~~
SELECT
RANK() OVER (PARTITION BY category ORDER BY sales DESC) AS ranking,
product_name, sales
FROM products;
~~~

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

What does the NTILE(n) window function do?

A

The NTILE(n) window function divides the result set into n number of partitions.
~~~
SELECT
NTILE(4) OVER (ORDER BY salary DESC) AS quartile,
employee_name, salary
FROM employees;
~~~

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

What is an example of the SUM(), AVG(), MIN(), MAX(), COUNT() window functions?

A
SELECT 
    department, 
    salary,
    AVG(salary) OVER (PARTITION BY department) AS avg_salary
FROM employees;
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What do the LEAD(column) and LAG(column) window functions do?

A

The LEAD() function access the next row in the column, and the LAG() function accesses the previous number in the column.
~~~
SELECT
employee_id,
salary,
LEAD(salary) OVER (ORDER BY hire_date) AS next_salary,
LAG(salary) OVER (ORDER BY hire_date) AS prev_salary
FROM employees;
~~~

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

What do the FIRST_VALUE() and LAST_VALUE() window functions do?

A

FIRST_VALUE() returns the first value in the window and LAST_VALUE() returns the last value in the window.
~~~
SELECT
employee_id,
salary,
FIRST_VALUE(salary) OVER (PARTITION BY department ORDER BY hire_date) AS first_salary,
LAST_VALUE(salary) OVER (PARTITION BY department ORDER BY hire_date) AS last_salary
FROM employees;
~~~

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