Raw Data
Calculating ( + )
Not only addition but also multiplication and division are possible.
select food_preparation_time,
delivery_time,
food_preparation_time + delivery_time as total_time
from food_orders
result
Sum & Average
select sum(food_preparation_time) total_food_preparation_time,
avg(delivery_time) avg_delivery_time
from food_orders
Count
By adding COUNT(1) or (*), you can count the number of rows for all columns. Adding DISTINCT allows you to count the number of distinct values for a specific column.
select count(1) count_of_orders,
count(distinct customer_id) count_of_customers
from food_orders
Max / Min
select min(price) min_price,
max(price) max_price
from food_orders
Group By / Order By
* "GROUP BY" and "SELECT BY" statements come after "SELECT", "FROM", and "WHERE"
"GROUP BY" is used to create categories, while "ORDER BY" is used for sorting.
Practice
Retrieve the highest and lowest order amounts for each type of food, and sort them in ascending order by the lowest order amount.
SELECT cuisine_type, MIN(price) min_price, max(price) max_price
from food_orders
group by cuisine_type
order by min(price) desc
Result
'Study Note > SQL' 카테고리의 다른 글
NULL value (0) | 2024.03.22 |
---|---|
Left Join & Inner Join (0) | 2024.03.21 |
Subquery (0) | 2024.03.21 |
Replace Data in SQL and IF Statements (0) | 2024.03.20 |
Understanding database and SQL (0) | 2024.03.19 |