Essential MYSQL functions Flashcards

1
Q

round 5.73

A
select round(5.73)
ROUND(n,d, f) -- If f is not zero, then the ROUND() function rounds n to the d number of decimal places.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

round 5.7345 within two digits from the decimal point

A

select round(5.7345, 2)

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

truncate 5.7345 two digits past the decimal

A

select truncate(5.7345, 2)

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

what is round is sql?

A

rounds a number to a specified number of decimal places

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

what is truncate in sql?

A
select truncate(5.7345, 2)
TRUNCATE(n, d) -- function returns n truncated to d decimal places.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

what is the ceiling of 5.2

A
select ceiling(5.3) #6
returns the smallest integer value that is larger than or equal to a number.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

what is the floor of 5.2?

A
select floor(5.2) # 5
returns the largest integer value that is smaller than or equal to a number
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

what is the absolute value of -5.2 in sql?

A
select abs(-5.2) # 5.2
returns the absolute value of a number
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

write a random value between 0 and 1 in sql?

A

select rand()

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

find the length of string ‘karen’

A

select length(‘karen’) # 5

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

change string ‘karen’ to upper-case

A

select upper(‘karen’) # KAREN

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

change string ‘KAREN’ to lowercase

A

select lower(‘KAREN’) # karen

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

remove the space from the string ‘ kelvin’

A

select ltrim(‘ kelvin’) # kelvin

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

remove the space from the string ‘kelvin ‘

A

select rtrim(‘kelvin ‘) # kelvin

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

remove any leading or trailing spaces from a string

A

select trim(‘ kelvin ‘) # kelvin

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

return the first four characters of string Kindergarten

A

select left(‘Kindergarten’, 4)

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

select the last for characters in string Kindergarten

A

select right(‘Kindergarten’, 4)

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

obtain the substring wat from kelvinwaters

A

select substring(‘kelvinwaters’, 7, 3) # start at 7 then highlights 3 more values

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

what is the first position of ‘n’ in ‘kindergarten’?

A

select locate(‘n’, ‘kindergarten’) # 3

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

True or False

when using the locate clause, capitalization isn’t valid

A

True

‘N’ is the same as ‘n’

21
Q

replace string Kindergarten with Kindergarden

A
select replace('Kindergarten', 'ten', 'den')
select replace('Kindergarten', 't', 'd')
22
Q

combine first and last name

A

select concat(‘kelvin’, ‘waters’)

23
Q

what is this query doing?

select concat(
first_name, ‘ ‘, last_name) as full_name
from customers

A

combining first and last name with a space between them

24
Q

what is the current date and time using mysql?

A

select now()

25
Q

what is the current time in mysql?

A

select curtime()

26
Q

how do you determine current year in mysql?

A

select year(now())

27
Q

how to determine the day name in mysql?

A

select dayname(now())

28
Q

what is the current day using the EXTRACT clause?

A

select extract(day from now())

29
Q

extract the year from today

A

select extract(year from now())

30
Q

refactor this code to return from the current year

select *
from orders
where order_date >= ‘2019-01-01’

A
select \*
from orders
where year(order\_date) = year(now())
31
Q

what is wrong or not advisable with this query?

select *
from orders
where order_date >= ‘2019-01-01’

A

you should not hard code the date

32
Q

use the date_format clause to return the date as

A

select date_format(now(), ‘%M %D %Y’)

33
Q

display the current time in format

13:11 PM

A

select time_format(now(), ‘%H:%i %p’)

34
Q

add a day to the current date time

A

select date_add(now(), interval(1) day)

35
Q

True or False

You can easily manipulate day and time queries by just changing the unit

A

True

36
Q

determine how many days I’ve been breathing

A

select datediff(now(), ‘1967-08-02’)

19789

37
Q

how would you change missing null values to something else, like not assigned?

A

select
order_id,
ifnull(shipper_id, ‘Not assigned’) as shipper
from orders

38
Q

what is the difference between ifnull and coalesce

A

ifnull we can select and alternative value

coalesce will return the first value that isn’t null, if both are null you can the alternative is selected.

39
Q

write this query

A

select
concat(first_name, ‘ ‘, last_name) as customer,
coalesce(phone, ‘Unknown’) as phone
from customers

40
Q

explain this query using the if clause

if(expression, first, second)

A

the expression is a boolean if True the first will be selected if False the second will be chosen

think if(expression, True, False)

41
Q

explain this query

select
order_id,
order_date,
if(
year(order_date) = year(now()),
‘active’,
‘archived’) as category
from orders

A

if order_date openeration is True, ‘active’ will be selected, if False ‘archived’ both under the category alias column

42
Q

write a query to produce this result

A

select
product_id,
name,
count(*) as orders,
if(count(*) > 1, ‘Many times’, ‘Once’) as frequency
from products
join order_items using (product_id)
group by product_id, name

43
Q

True or False

A CASE statement must end with the END clause

A

True

44
Q

In this query what is the order of operation on the case statement

A

the first WHEN boolean returns True before proceeding to the next WHEN clause

45
Q

If you have multiple test expression what is the sql statement to use?

A

the CASE statement

46
Q

True or False

It’s good practice to name an alias to a CASE statement

A

True

47
Q

what is the sql syntax to end a CASE statement and assign it an alias?

A

END AS ‘alias_name

48
Q

write a query using the CASE statement to produce this result

A

select
concat(first_name,’ ‘, last_name) as customer,
points,
case
when points > 3000 then ‘Gold’
when points >= 2000 then ‘Silver’
else ‘Bronze’
end as category
from customers