PostgreSQL Cheatsheet
•2 snippets
A comprehensive guide to PostgreSQL with examples for queries, transactions, and more.
Querying with JSON Aggregates
Create a new database with the name 'my_database'.
1SELECT
2 json_agg(
3 json_build_object(
4 'id', id,
5 'name', name,
6 'age', age,
7 'carModel', carModel,
8 'carYear', carYear
9 ) as person inner join cars on users.id = cars.userId GROUP BY id ORDER BY carCount DESC;
Relative date interval queries
Query to get all users who have been active in the last 30 days
1SELECT * FROM users WHERE last_active > NOW() - INTERVAL '30 days';