SQL Advanced Flashcards
(42 cards)
What does this query do?
SELECT companies.permalink,
companies.founded_at_clean,
acquisitions.acquired_at_cleaned,
acquisitions.acquired_at_cleaned -
companies.founded_at_clean::timestamp AS time_to_acquisition
FROM tutorial.crunchbase_companies_clean_date companies
JOIN tutorial.crunchbase_acquisitions_clean_date acquisitions
ON acquisitions.company_permalink = companies.permalink
WHERE founded_at_clean IS NOT NULL

What does this query do?
SELECT companies.permalink,
companies.founded_at_clean,
companies.founded_at_clean::timestamp +
INTERVAL ‘1 week’ AS plus_one_week
FROM tutorial.crunchbase_companies_clean_date companies
WHERE founded_at_clean IS NOT NULL

Interval
The interval is defined using plain-English terms like ‘10 seconds’ or ‘5 months’. Also note that adding or subtracting a date column and an interval column results in another date column as in the above query.
NOW()
get the current server time
SELECT companies.permalink, companies.founded_at_clean, NOW() - companies.founded_at_clean::timestamp AS founded_time_ago FROM tutorial.crunchbase_companies_clean_date companies WHERE founded_at_clean IS NOT NULL

Write a query that counts the number of companies acquired within 3 years, 5 years, and 10 years of being founded (in 3 separate columns). Include a column for total companies acquired as well. Group by category and limit to only rows with a founding date.
SELECT companies.category_code,
COUNT(CASE WHEN acquisitions.acquired_at_cleaned <= companies.founded_at_clean::timestamp + INTERVAL ‘3 years’
THEN 1 ELSE NULL END) AS acquired_3_yrs,
COUNT(CASE WHEN acquisitions.acquired_at_cleaned <= companies.founded_at_clean::timestamp + INTERVAL ‘5 years’
THEN 1 ELSE NULL END) AS acquired_5_yrs,
COUNT(CASE WHEN acquisitions.acquired_at_cleaned <= companies.founded_at_clean::timestamp + INTERVAL ‘10 years’
THEN 1 ELSE NULL END) AS acquired_10_yrs,
COUNT(1) AS total
FROM tutorial.crunchbase_companies_clean_date companies
JOIN tutorial.crunchbase_acquisitions_clean_date acquisitions
ON acquisitions.company_permalink = companies.permalink
WHERE founded_at_clean IS NOT NULL
GROUP BY 1
ORDER BY 5 DESC
LEFT
You can use LEFT to pull a certain number of characters from the left side of a string and present them as a separate string. The syntax is LEFT(string, number of characters).
SELECT incidnt_num,
date,
LEFT(date, 10) AS cleaned_date
FROM tutorial.sf_crime_incidents_2014_01
What will this do with this database?

It will take the first 10 characters from the date column and move them to cleaned_date a string

RIGHT
same as left but works from the right side
SELECT incidnt_num, date, LEFT(date, 10) AS cleaned_date, RIGHT(date, 17) AS cleaned_time FROM tutorial.sf_crime_incidents_2014_01

LENGTH()
returns the length of a string
SELECT incidnt_num, date, LEFT(date, 10) AS cleaned_date, RIGHT(date, LENGTH(date) - 11) AS cleaned_time FROM tutorial.sf_crime_incidents_2014_01
What will this produce with this database?

inner functions are always evaluated first

TRIM
The TRIM function takes 3 arguments. First, you have to specify whether you want to remove characters from the beginning (‘leading’), the end (‘trailing’), or both (‘both’, as used above). Next you must specify all characters to be trimmed. Any characters included in the single quotes will be removed from both beginning, end, or both sides of the string. Finally, you must specify the text you want to trim using FROM.
SELECT location, TRIM(both ‘()’ FROM location) FROM tutorial.sf_crime_incidents_2014_01
What will this do in this database?


POSITION
POSITION allows you to specify a substring, then returns a numerical value equal to the character number (counting from left) where that substring first appears in the target string
POSITION(‘A’ IN position)
SELECT incidnt_num,
descript,
POSITION(‘A’ IN descript) AS a_position
FROM tutorial.sf_crime_incidents_2014_01
What will this do in this database?


SUBSTR
SUBSTR(*string*, *starting character position*, *# of characters*):
removes strings at a particular location
SELECT incidnt_num, date,
SUBSTR(date, 4, 2) AS day
FROM tutorial.sf_crime_incidents_2014_01


Write a query that separates the location
field into separate fields for latitude and longitude.

SELECT location,
TRIM(leading ‘(‘ FROM LEFT(location, POSITION(‘,’ IN location) - 1)) AS lattitude,
TRIM(trailing ‘)’ FROM RIGHT(location, LENGTH(location) - POSITION(‘,’ IN location) ) )
AS longitude
FROM tutorial.sf_crime_incidents_2014_01

CONCAT
Simply order the values you want to concatenate and separate them with commas. If you want to hard-code values, enclose them in single quotes.
SELECT incidnt_num, day_of_week,
LEFT(date, 10) AS cleaned_date, CONCAT(day_of_week, ‘, ‘, LEFT(date, 10)) AS day_and_date
FROM tutorial.sf_crime_incidents_2014_01
What will this produce?


Concatenate the lat and lon fields to form a field that is equivalent to the location field. (Note that the answer will have a different decimal precision.)

SELECT lat,
lon,
location,
CONCAT(‘(‘,lat,’, ‘,lon,’)’) AS concat_loc
FROM tutorial.sf_crime_incidents_2014_01

Create the same concatenated location field from lat and lon, but using the || syntax instead of CONCAT.

SELECT lat,
lon,
location,
‘(‘ || lat || ‘,’ || lon || ‘)’ AS concat_loc
FROM tutorial.sf_crime_incidents_2014_01
Write a query that creates a date column formatted YYYY-MM-DD.

SELECT date,
CONCAT(SUBSTR(date, 7, 4), ‘-‘, SUBSTR(date, 1, 2), ‘-‘, SUBSTR(date, 4, 2)) AS con_date
FROM tutorial.sf_crime_incidents_2014_01

Changing case with UPPER and LOWER
SELECT incidnt_num, address, UPPER(address) AS address_upper, LOWER(address) AS address_lower FROM tutorial.sf_crime_incidents_2014_01

Write a query that returns the category
field, but with the first letter capitalized and the rest of the letters in lower-case.
SELECT incidnt_num, category, UPPER(LEFT(category, 1)) || LOWER(RIGHT(category, LENGTH(category) - 1)) AS category_cleaned FROM tutorial.sf_crime_incidents_2014_01












