Data Science/SQL

[Udemy] 15 Days of SQL - Day 1: ORDER BY, SELECT, LIMIT, COUNT

kyra 2024. 1. 26. 13:48

ORDER BY

  • ORDER BY 'COLUMN' 
  • ASC - default, DESC
  • using index number instead of column names

SELECT DISTINCT

  • Eliminate duplicated values
  • more than two columns - considering all columns and show only unique values
  • Find out unique values in columns

*Challenge

1. show different prices that have been paid
2. order the prices from high to low

 

LIMIT

  • Used to limit the number of rows in the output
  • Always at the very end of your query
  • Can help to get a quick idea about a table

 

COUNT

  • Count the number of rows in a output
  • Used often in combination with grouping & filtering
  • Nulls will not be counted
  • Use with DISTINCT - count only unique values

Day 1 Challenge

1) Create a list of all the distinct districts customers are from.

SELECT DISTINCT district
FROM address

 

2) What is the latest rental date?

SELECT rental_date
FROM rental
ORDER BY rental_date DESC
LIMIT 1

 

3) How many films does the company have?

SELECT 
COUNT(*)
FROM film

4) How many distinct last names of the cutomers are there?

SELECT 
COUNT(DISTINCT last_name)
FROM customer