LENGTH ()
REGEXP"^[aeiou]", "[aeiou]$", "^[aeiou].*[aeiou]$", "^[^aeiou]", "[^aeiou]$",
SUBSTRING ()
CONCAT ()
LOWER ()
CEIL () ROUND () FLOOR ()
INTERVAL “1 day”
SELECT DISTINCT CITY FROM STATION WHERE ID % 2 = 0;
SELECT (COUNT(CITY) - COUNT(DISTINCT CITY)) FROM STATION;
SELECT (MAX(POPULATION)-MIN(POPULATION)) FROM CITY
SELECT CITY, LENGTH(CITY) FROM STATION ORDER BY LENGTH(CITY) ASC, CITY ASC LIMIT 1;
SELECT CITY, LENGTH(CITY) FROM STATION ORDER BY LENGTH (CITY) DESC, CITY DESC LIMIT 1;
SELECT DISTINCT CITY FROM STATION WHERE CITY REGEXP "^[aeiou]";
SELECT DISTINCT CITY FROM STATION WHERE CITY REGEXP "[aeiou]$";
SELECT DISTINCT CITY FROM STATION WHERE CITY REGEXP "^[aeiou].*[aeiou]$"
SELECT DISTINCT CITY FROM STATION WHERE CITY REGEXP "^[^aeiou]";
SELECT DISTINCT CITY FROM STATION WHERE CITY REGEXP "[^aeiou]$";
SELECT DISTINCT CITY FROM STATION WHERE CITY REGEXP '^[^AEIOU]' OR CITY REGEXP '[^AEIOU]$';
SELECT NAME FROM STUDENTS WHERE MARKS > 75 ORDER BY SUBSTRING(NAME, -3), ID;
SELECT name FROM Employee WHERE salary > 2000 AND months < 10 ORDER BY employee_id
SELECT CASE
WHEN A + B > C AND B + C > A AND A + C > B THEN
CASE
WHEN A = B AND B = C THEN 'Equilateral'
WHEN A = B OR B = C OR A = C THEN 'Isosceles'
ELSE 'Scalene'
END
ELSE 'Not A Triangle'
END
FROM TRIANGLES;
SELECT CONCAT(Name,"(", SUBSTRING(Occupation, 1, 1), ")") AS A FROM OCCUPATIONS ORDER BY
A;
SELECT CONCAT('There are a total of ', COUNT(Occupation)," ", LOWER(Occupation),'s.') AS B FROM
OCCUPATIONS GROUP BY Occupation ORDER BY B
SELECT CEIL(AVG(Salary)-AVG(REPLACE(Salary,'0',''))) FROM EMPLOYEES;
SELECT ROUND(AVG(POPULATION))FROM CITY;
SELECT FLOOR(AVG(POPULATION))FROM CITY;
SELECT (salary*months) as earnings, count(*) FROM Employee GROUP BY 1 ORDER BY earnings DESC
LIMIT 1
SELECT ROUND(SUM(LAT_N),2), ROUND(SUM(LONG_W),2) FROM STATION
SELECT TRUNCATE(MAX(LAT_N),4) FROM STATION WHERE LAT_N < 137.2345
SELECT ROUND(LONG_W,4) FROM STATION WHERE LAT_N = (SELECT MIN(LAT_N) FROM STATION
WHERE LAT_N > 38.7780)
SELECT SUM(CITY.POPULATION) FROM CITY INNER JOIN COUNTRY ON CITY.CountryCode =
COUNTRY.Code WHERE COUNTRY.CONTINENT = "Asia"
SELECT COUNTRY.Continent, FLOOR(AVG(CITY.POPULATION)) FROM CITY INNER JOIN COUNTRY ON
CITY.CountryCode = COUNTRY.Code GROUP BY 1
SELECT tweet_count as tweet_bucket, COUNT(user_id) as users_num FROM
(SELECT user_id, COUNT(tweet_id) AS tweet_count FROM tweets WHERE tweet_date BETWEEN
'2022-01-01'
AND '2022-12-31' GROUP BY user_id)
AS total_tweets GROUP BY tweet_count
SELECT candidate_id, skill FROM candidates WHERE skill IN ('Python', 'Tableau', 'PostgreSQL')
SELECT candidate_id
FROM candidates
WHERE skill IN ('Python', 'Tableau', 'PostgreSQL')
GROUP BY candidate_id
HAVING COUNT(skill) = 3
ORDER BY candidate_id;
SELECT pages.page_id
FROM pages
LEFT JOIN page_likes
ON pages.page_id = page_likes.page_id
WHERE page_likes.page_id IS NULL;
SELECT page_id
FROM pages
EXCEPT
SELECT page_id
FROM page_likes;
SELECT part, assembly_step FROM parts_assembly WHERE finish_date IS NULL
SELECT
COUNT(*) FILTER (WHERE device_type = 'laptop') as laptop_views,
COUNT(*) FILTER (WHERE device_type IN ('tablet', 'phone')) AS mobile_views
FROM viewership
SELECT
user_id,
MAX(post_date::DATE) - MIN(post_date::DATE) AS days_between
FROM posts
WHERE DATE_PART('year', post_date::DATE) = 2021
GROUP BY user_id
HAVING COUNT(post_id)>1;
SELECT sender_id, COUNT(sender_id)
FROM messages
WHERE EXTRACT(MONTH FROM sent_date) = '8'
AND EXTRACT(YEAR FROM sent_date) = '2022'
GROUP BY sender_id
ORDER BY 2 DESC
LIMIT 2
SELECT city, COUNT(city) FROM trades
LEFT JOIN users ON trades.user_id = users.user_id
WHERE trades.status = 'Completed'
GROUP BY city
ORDER BY COUNT(city) DESC
LIMIT 3
SELECT EXTRACT(MONTH FROM submit_date), product_id, ROUND(AVG(stars),2)
FROM reviews
GROUP BY product_id, 1 ORDER BY 1, 2;
SELECT
app_id,
ROUND(100.0 *
SUM(app_id) FILTER (WHERE event_type = 'click') /
SUM(app_id) FILTER (WHERE event_type = 'impression'), 2) AS ctr_app
FROM events
WHERE timestamp >= '2022-01-01'
AND timestamp < '2023-01-01'
GROUP BY app_id;
SELECT user_id
FROM emails
JOIN texts ON
emails.email_id = texts.email_id
WHERE texts.action_date = emails.signup_date + INTERVAL '1 day'
AND texts.signup_action = 'Confirmed';
SELECT ROUND((SUM(order_occurrences*item_count::DECIMAL)/SUM(order_occurrences)),1)
FROM items_per_order;
SELECT card_name, (MAX(issued_amount) - MIN(issued_amount)) as difference
FROM monthly_cards_issued GROUP BY card_name
ORDER BY difference DESC
SELECT drug, SUM(total_sales - cogs)
FROM pharmacy_sales
GROUP BY 1
ORDER BY 2
DESC LIMIT 3
SELECT manufacturer, COUNT(drug), ABS(SUM(total_sales - cogs)) AS total_loss
FROM pharmacy_sales WHERE cogs > total_sales
GROUP BY manufacturer ORDER BY total_loss DESC
SELECT manufacturer, CONCAT( '$', ROUND(SUM(total_sales) / 1000000), ' million') AS sales_mil
FROM pharmacy_sales GROUP BY manufacturer ORDER BY SUM(total_sales) DESC, manufacturer;
SELECT
user_id,
spend,
transaction_date
FROM (
SELECT
user_id,
spend,
transaction_date,
ROW_NUMBER() OVER (
PARTITION BY user_id ORDER BY transaction_date) AS row_num
FROM transactions) AS trans_num
WHERE row_num = 3;