0% found this document useful (0 votes)
68 views1 page

PostgreSQL Cheat Sheet

PostgreSQL is an open-source relational database management system known for its extensibility and standards compliance. It has powerful data analysis capabilities. The document provides examples of using SQL commands like SELECT, WHERE, GROUP BY, and HAVING to filter, aggregate, group and sort data in PostgreSQL. Sample car performance data is also shown to demonstrate these SQL queries.

Uploaded by

BilalAhmed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
68 views1 page

PostgreSQL Cheat Sheet

PostgreSQL is an open-source relational database management system known for its extensibility and standards compliance. It has powerful data analysis capabilities. The document provides examples of using SQL commands like SELECT, WHERE, GROUP BY, and HAVING to filter, aggregate, group and sort data in PostgreSQL. Sample car performance data is also shown to demonstrate these SQL queries.

Uploaded by

BilalAhmed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

> Filtering Data > Aggregating Data

Filtering on numeric columns Simple aggregations

PostgreSQL Basics Get rows where a number is greater than a value with WHERE col > n
SELECT make, model, time_to_60_mph_s

Get the total number of rows SELECT COUNT(*)


SELECT COUNT(*)

FROM cars

FROM cars
WHERE time_to_60_mph_s > 2.1
Learn SQL online at www.DataCamp.com Get rows where a number is greater than or equal to a value with WHERE col >= n Get the total value of a column with SELECT SUM(col)

SELECT make, model, time_to_60_mph_s


SELECT SUM(limited_production_count)

FROM cars
FROM cars
WHERE time_to_60_mph_s >= 2.1
Get the mean value of a column with SELECT AVG(col)
Get rows where a number is less than a value with WHERE col < n
What is PostgreSQL? SELECT make, model, time_to_60_mph_s

SELECT AVG(time_to_60_mph_s)

FROM cars
FROM cars

PostgreSQL is an open-source relational database management system (RDBMS) known for its WHERE time_to_60_mph_s < 2.1 Get the minimum value of a column with SELECT MIN(col)
extensibility and standards compliance. Developed and maintained by a group of volunteers Get rows where a number is less than or equal to a value with WHERE col <= n SELECT MIN(time_to_60_mph_s)

known as The PostgreSQL Global Development Group, it is popular across a wide range of FROM cars
SELECT make, model, time_to_60_mph_s

organizations from enterprises to government departments. It has powerful data analysis FROM cars
Get the maximum value of a column with SELECT MAX(col)
WHERE time_to_60_mph_s <= 2.1
capabilities. SELECT MAX(time_to_60_mph_s)

Get rows where a number is equal to a value with WHERE col = n FROM cars
SELECT make, model, time_to_60_mph_s

>
FROM cars

Sample Data WHERE time_to_60_mph_s = 2.1 Grouping, filtering, and sorting


Get rows where a number is not equal to a value with WHERE col <> n or WHERE col != n
The dataset contains details of the world's fastest production cars by 0 to 60 mph acceleration Get summaries grouped by values with GROUP BY col
SELECT make, model, time_to_60_mph_s

time. Each row contains one car model, and the table is named cars. FROM cars
SELECT propulsion_type, COUNT(*)

WHERE time_to_60_mph_s <> 2.1 FROM cars

GROUP BY propulsion_type
make model year propulsion_type time_to_60_mph_s limited_production_count Get rows where a number is between two values (inclusive) with WHERE col BETWEEN m AND n
Lamborghini Huracán Performante 2018 ICE 2.2 SELECT make, model, time_to_60_mph_s
Get summaries grouped by values, in order of summaries with GROUP BY col ORDER BY smmry DESC
FROM cars
SELECT propulsion_type, AVG(time_to_60_mph_s) AS mean_time_to_60_mph_s

Ferrari SF90 Stradale 2021 Hybrid 2


WHERE time_to_60_mph_s BETWEEN 1.9 AND 2.1 FROM cars

Tesla Model S Plaid 2021 Electric 1.98 GROUP BY propulsion_type

Porsche 918 Spyder 2015 Hybrid 2.1 918 Filtering on text columns ORDER BY mean_time_to_60_mph_s
Rimac Nevera 2021 Electric 1.74 150 Get rows where values in a group meet a criterion with GROUP BY col HAVING condn
Porsche 911 Turbo S (992) 2020 ICE Get rows where text is equal to a value with WHERE col = 'x'
2.1 SELECT propulsion_type, AVG(time_to_60_mph_s) AS mean_time_to_60_mph_s

SELECT make, model, propulsion_type


FROM cars

FROM cars
GROUP BY propulsion_type

WHERE propulsion_type = 'Hybrid' HAVING mean_time_to_60_mph_s > 2

> Querying tables Get rows where text is one of several values with WHERE col IN ('x', 'y') Filter before and after grouping with WHERE condn_before GROUP BY col HAVING condn_after
SELECT make, model, propulsion_type
SELECT propulsion_type, AVG(time_to_60_mph_s) AS mean_time_to_60_mph_s

Get all the columns from a table using SELECT * FROM cars
FROM cars

WHERE propulsion_type IN ('Electric', 'Hybrid') WHERE limited_production_count IS NOT NULL

SELECT *

FROM cars Get rows where text contains specific letters with WHERE col LIKE '%abc%' (% represents any characters) GROUP BY propulsion_type

HAVING mean_time_to_60_mph_s > 2


Get a column from a table by name using SELECT col SELECT make, model, propulsion_type

FROM cars

SELECT model

WHERE propulsion_type LIKE '%ic%'

>
FROM cars
Get multiple columns from a table by name using SELECT col1, col2
For case insensitive matching, use WHERE col ILIKE '%abc%' PostgreSQL-Specific Syntax
SELECT make, model, propulsion_type

SELECT make, model

FROM cars
Not all code works in every dialect of SQL. The following examples work in PostgreSQL, but are not
FROM cars
WHERE propulsion_type ILIKE '%ic%'
guaranteed to work in other dialects.
Override column names with SELECT col AS new_name
Limit the number of rows returned, offset from the top with LIMIT m OFFSET n
SELECT make, model, propulsion_type AS engine_type
Filtering on multiple columns
FROM cars SELECT *

Override column names with SELECT col AS new_name FROM cars

Get the rows where one condition and another condition holds with WHERE condn1 AND condn2
LIMIT 2 OFFSET 3
SELECT make, model, propulsion_type
SELECT make, model, propulsion_type, model_year

AS engine_type
FROM cars
PostgreSQL allows text concatenation with the || operator
FROM cars WHERE propulsion_type = 'Hybrid' AND model_year < 2020 SELECT make || ' ' || model AS make_and_model

Arrange the rows in ascending order of values in a column with ORDER BY col Get the rows where one condition or another condition holds with WHERE condn1 OR condn2 FROM cars
SELECT make, model, time_to_60_mph_s
SELECT make, model, propulsion_type, model_year
Get the current date with CURRENT_DATE and the current datetime with NOW() or CURRENT_TIME
FROM cars
FROM cars

ORDER BY time_to_60_mph_s WHERE propulsion_type = 'Hybrid' OR model_year < 2020 SELECT NOW(), CURRENT_DATE, CURRENT_TIME
Arrange the rows in descending order of values in a column with ORDER BY col DESC List available tables by selecting from pg_catalog.pg_tables
SELECT make, model, model_year
Filtering on missing data SELECT *

FROM cars
FROM pg_catalog.pg_tables
ORDER BY model_year DESC Get rows where values are missing with WHERE col IS NULL
Limit the number of rows returned with LIMIT n SELECT make, model, limited_production_count

SELECT *
FROM cars

FROM cars
WHERE limited_production_count IS NULL
LIMIT 2
Get rows where values are not missing with WHERE col IS NOT NULL
Get unique values with SELECT DISTINCT
SELECT make, model, limited_production_count

SELECT DISTINCT propulsion_type


FROM cars

FROM cars WHERE limited_production_count IS NOT NULL

You might also like