Functions Flashcards

1
Q

Return column ‘first_name’ in all upper-case

A

SELECT UPPER(first_name) FROM employees

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

Return column ‘first_name’ in all upper-case

A

SELECT LOWER(first_name) FROM employees

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

Return the length of each value in column ‘first_name’

A

SELECT LENGTH(first_name) FROM employees

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

Trim the whitespace around the first_name values

A

SELECT TRIM(first_name) FROM employees

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

Combine columns first_name and last_name into one column called name (including a space between the two)

A

SELECT first_name ||’ ‘|| last_name FROM employees

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

What does SELECT first_name, (salary > 140000) FROM employees return?

A

It returns two columns: one which is first_name, and the other which is a column of booleans which return True or False depending on if the row’s ‘salary’ value is greater/less than 140000

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

What happens when you execute this:
SELECT SUBSTRING(‘This is test data’ FROM 1 FOR 4)

A

It will return 4 characters starting at the first character, which is ‘This’

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

What is the purpose of ‘SUBSTRING()’?

A

It will return a substring based on a starting location and the number of characters following it, e.g.
SELECT SUBSTRING(‘Hello’ FROM 2 FOR 2) –> el

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

Replace ‘Clothing’ values with ‘Attire’ in the department column in your SELECT statement

A

SELECT department, REPLACE(department, ‘Clothing’, ‘Attire’)
FROM departments

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

Return the domain names of all the emails in the ‘email’ column

A

SELECT SUBSTRING(email, POSITION(‘@’ IN email) + 1)

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

What does POSITION() do?

A

it returns the positions of the specified value in a column, i.e.
POSITION(‘value’ IN column)

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

What does COALESCE() do?

A

It fills nan values with a specified value in a specified column, i.e.
COALESCE(email, ‘Hi’)

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

Return the max, min, and rounded average of the salary column from employees

A
SELECT ROUND(AVG(salary)) average,
MAX(salary) maximum,
MIN(salary) minimum
FROM employees
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Return the number of rows in employees

A
SELECT COUNT(*)
FROM employees
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

T/F: COUNT() will include the nans in a column in its count

A

False, it will skip nans

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

What does LENGTH() do?

A

Returns the length of a string

17
Q

Write a WHERE clause that subtracts two dates and returns the difference in days

A

WHERE DATE_PART('day', w1.recorddate) - DATE_PART('day', w2.recorddate) = 1

18
Q

T/F: you can insert data into views

A

False, views are read-only

19
Q

What is the clause to create a view?

A

CREATE VIEW v_my_table AS …