Essential MYSQL functions Flashcards

(48 cards)

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?

25
what is the current time in mysql?
select curtime()
26
how do you determine current year in mysql?
select year(now())
27
how to determine the day name in mysql?
select dayname(now())
28
what is the current day using the EXTRACT clause?
select extract(day from now())
29
extract the year from today
select extract(year from now())
30
refactor this code to return from the current year ## Footnote select \* from orders where order\_date \>= '2019-01-01'
``` select \* from orders where year(order\_date) = year(now()) ```
31
what is wrong or not advisable with this query? ## Footnote select \* from orders where order\_date \>= '2019-01-01'
you should not hard code the date
32
use the date\_format clause to return the date as
select date\_format(now(), '%M %D %Y')
33
display the current time in format ## Footnote 13:11 PM
select time\_format(now(), '%H:%i %p')
34
add a day to the current date time
select date\_add(now(), interval(1) day)
35
True or False You can easily manipulate day and time queries by just changing the unit
True
36
determine how many days I've been breathing
select datediff(now(), '1967-08-02') ## Footnote 19789
37
how would you change missing null values to something else, like not assigned?
select order\_id, ifnull(shipper\_id, 'Not assigned') as shipper from orders
38
what is the difference between ifnull and coalesce
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
write this query
select concat(first\_name, ' ', last\_name) as customer, coalesce(phone, 'Unknown') as phone from customers
40
explain this query using the if clause if(expression, first, second)
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
explain this query ## Footnote select order\_id, order\_date, if( year(order\_date) = year(now()), 'active', 'archived') as category from orders
if order\_date openeration is True, ‘active’ will be selected, if False ‘archived’ both under the category alias column
42
write a query to produce this result
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
True or False A CASE statement must end with the END clause
True
44
In this query what is the order of operation on the case statement
the first WHEN boolean returns True before proceeding to the next WHEN clause
45
If you have multiple test expression what is the sql statement to use?
the CASE statement
46
True or False It's good practice to name an alias to a CASE statement
True
47
what is the sql syntax to end a CASE statement and assign it an alias?
END AS ‘*alias\_name*’
48
write a query using the CASE statement to produce this result
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