0% found this document useful (0 votes)
65 views

Rebellabs SQL Cheat Sheeasdasdasd55555555

This document provides a summary of basic SQL queries and concepts: - It describes common SQL clauses like SELECT, WHERE, GROUP BY, HAVING, ORDER BY, and LIMIT that are used to filter and aggregate data. - It also covers JOINs, different JOIN types, and using subqueries as an alternative to JOINs. - Data modification statements like UPDATE, INSERT, and common functions used during inserts and updates are summarized. - Other concepts covered at a high level include views, indexes, and aggregation functions used for reporting.

Uploaded by

cueva
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
65 views

Rebellabs SQL Cheat Sheeasdasdasd55555555

This document provides a summary of basic SQL queries and concepts: - It describes common SQL clauses like SELECT, WHERE, GROUP BY, HAVING, ORDER BY, and LIMIT that are used to filter and aggregate data. - It also covers JOINs, different JOIN types, and using subqueries as an alternative to JOINs. - Data modification statements like UPDATE, INSERT, and common functions used during inserts and updates are summarized. - Other concepts covered at a high level include views, indexes, and aggregation functions used for reporting.

Uploaded by

cueva
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

Basic Queries TheJoy ofJdsadasdasdasADdOINS

-- filter your columns


SELECT col 1, col2, col3, ... FROM table1
-- filter the rowsSDASDASDewe we e
wadSDasd
WHERE col4 = 1 ANDAsaDsaDADD
cols= 2
-- aggregate the dadaSDasdADadta
GROUP by ...
-- limit aggregated data
HAVING count(*) > 1
LEFT OUTER JOIN - al! rows from table A /NNERJOIN -fetch the results that RIGHT OUTER JOIN - al! rows from table 8,
-- O
order
ROERof B
t hYecol
r2esults
even 1/ they do not exist in table B exist in bath tables even if they do not exist in table A

Useful keywords for SELECTS:


DISTINCT - return unique results Updates odasdasdaSDn • ctions
BETWEEN a AND b - limit dasdASDasdADadASDthe JOINed Queries -- convert strings to dates:
range, the values can be numbers, text, or dates
You can use JOINs in your UPDATEs TO_DATE (Oracle, PostgreSQL), STR_TO_DATE (MySQL)
LIKE - pattern search within the column text -- return the first non-NULL argument:
IN (a, b, c) - check if the value is contained among given. UPDATE t1 SET a = 1
FROM table1 t1 JOIN table2 t2 ON t1.id = t2.t1_id COALESCE (col1, col2, "default value")
WHERE t1.col1 = 0 AND t2.col2 IS NULL; -- return current time:
CURRENT_TIMESTAMP
Data Modification -- compute set operations on two result sets
NBI Use database specific syntax, it might be faster!
-- update specific data with the WHERE clause SELECT col1, col2 FROM table1
UPDATE table1 SET col1 = 1 WHERE col2 = 2 UNION/ EXCEPT/ INTERSECT
-- insert
INSERTvalues
INTOmanually
table1 (ID, FIRST_NAME, LAST_NAME)
SemiJOINs SELECT col3, col4 FROM table2;
VALUES (1, 'Rebel', 'Labs'); You can use subqueries instead of JOINs:
-- or by using the results of a query (SELECT t1_id FROM table2 WHERE date> Union - returns data from both queries
SELECT col1, col2 FROM table1 WHERE id IN
INSERT INTO table1 (ID, FIRST_NAME, LAST_NAME) CURRENT_Tl MESTAMP) Except - rows from the first query that are not present
SELECT id, last_name, first_name FROM table2 in the second query
lntersect- rows that are returned from both queries
Indexes
Views If you query by a column, index it!
A VIEW is a virtual table, which is a result of a query. CREATE INDEX index1 ON table1 (col1) Reporting
They can be used to create virtual tables of complex queries. Use aggregation functions
Don't forget:
CREATE VIEW view1 AS COUNT - return the number of rows
SELECT col1, col2 Avoid overlapping indexes SUM - cumulate the values
FROM table1 Avoid indexing on too many columns AVG - return the average for the group
WHERE ... Indexes can speed up DELETE and UPDATE operations MIN/ MAX - smallest / largest value BROUGHT TO YOU BY

XRebel

You might also like