This SQL cheat sheet provides essential commands for querying data from tables, including selecting, filtering, and joining data. It covers various SQL operators such as UNION, INTERSECT, and MINUS, as well as sorting and limiting results. Additionally, it includes aggregate functions and conditions for checking NULL values and pattern matching.
This SQL cheat sheet provides essential commands for querying data from tables, including selecting, filtering, and joining data. It covers various SQL operators such as UNION, INTERSECT, and MINUS, as well as sorting and limiting results. Additionally, it includes aggregate functions and conditions for checking NULL values and pattern matching.
org QUERYING DATA FROM A TABLE QUERYING FROM MULTIPLE TABLES USING SQL OPERATORS
SELECT c1, c2 FROM t; SELECT c1, c2 SELECT c1, c2 FROM t1
Query data in columns c1, c2 from a table FROM t1 UNION [ALL] INNER JOIN t2 ON condition; SELECT c1, c2 FROM t2; SELECT * FROM t; Inner join t1 and t2 Combine rows from two queries Query all rows and columns from a table SELECT c1, c2 SELECT c1, c2 FROM t1 SELECT c1, c2 FROM t FROM t1 INTERSECT WHERE condition; LEFT JOIN t2 ON condition; SELECT c1, c2 FROM t2; Query data and filter rows with a condition Left join t1 and t1 Return the intersection of two queries
SELECT DISTINCT c1 FROM t SELECT c1, c2
WHERE condition; FROM t1 SELECT c1, c2 FROM t1 Query distinct rows from a table RIGHT JOIN t2 ON condition; MINUS Right join t1 and t2 SELECT c1, c2 FROM t2; Subtract a result set from another result set SELECT c1, c2 FROM t ORDER BY c1 ASC [DESC]; SELECT c1, c2 Sort the result set in ascending or descending FROM t1 SELECT c1, c2 FROM t1 order FULL OUTER JOIN t2 ON condition; WHERE c1 [NOT] LIKE pattern; Perform full outer join Query rows using pattern matching %, _ SELECT c1, c2 FROM t ORDER BY c1 SELECT c1, c2 SELECT c1, c2 FROM t LIMIT n OFFSET offset; FROM t1 WHERE c1 [NOT] IN value_list; Skip offset of rows and return the next n rows CROSS JOIN t2; Query rows in a list Produce a Cartesian product of rows in tables SELECT c1, aggregate(c2) FROM t SELECT c1, c2 SELECT c1, c2 FROM t GROUP BY c1; FROM t1, t2; WHERE c1 BETWEEN low AND high; Group rows using an aggregate function Another way to perform cross join Query rows between two values
SELECT c1, aggregate(c2) SELECT c1, c2 SELECT c1, c2 FROM t
FROM t FROM t1 A WHERE c1 IS [NOT] NULL; GROUP BY c1 INNER JOIN t2 B ON condition; Check if values in a table is NULL or not HAVING condition; Join t1 to itself using INNER JOIN clause Filter groups using HAVING clause