Skip to content

Instantly share code, notes, and snippets.

@codecademydev
Created July 24, 2021 09:29
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save codecademydev/6d7bfac876335c9d6011e23a4d0bafe4 to your computer and use it in GitHub Desktop.
Save codecademydev/6d7bfac876335c9d6011e23a4d0bafe4 to your computer and use it in GitHub Desktop.
Codecademy export
/* 1 */
SELECT * FROM state_climate;
/* 2 */
SELECT state, year, tempc,
AVG(tempc) OVER (
PARTITION BY state
ORDER BY year
) AS 'running_avg_temp'
FROM state_climate;
/* 3 */
SELECT state, year, tempc,
FIRST_VALUE(tempc) OVER (
PARTITION BY state
ORDER BY tempc
) AS 'lowest_temp'
FROM state_climate;
/* 4 */
SELECT state, year, tempc,
LAST_VALUE(tempc) OVER (
PARTITION BY state
ORDER BY tempc
RANGE BETWEEN UNBOUNDED PRECEDING AND
UNBOUNDED FOLLOWING
) AS 'highest_temp'
FROM state_climate;
/* 5 */
SELECT state, year, tempc,
tempc - LAG(tempc,1,0) OVER (
PARTITION BY state
ORDER BY year
) AS 'change_in_temp'
FROM state_climate
ORDER BY change_in_temp DESC;
/* 6 */
SELECT
RANK() OVER (
ORDER BY tempc
) AS 'coldest_rank',
state, year, tempc
FROM state_climate;
/* 7 */
SELECT
RANK() OVER (
PARTITION BY state
ORDER BY tempc DESC
) AS 'warmest_rank',
state, year, tempc
FROM state_climate;
/* 8 */
SELECT
NTILE(4) OVER (
PARTITION BY state
ORDER BY tempc
) AS 'quartile',
state, year, tempc
FROM state_climate;
/* 9 */
SELECT
NTILE(5) OVER (
ORDER BY tempc
) AS 'quintile',
state, year, tempc
FROM state_climate;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment