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

Rebellabs SQL Cheat Sheeasdasdasd2222222t

The document provides an overview of basic SQL queries including selecting, filtering, aggregating, ordering and limiting data as well as joining tables. It also covers updating, inserting and deleting data along with using views, indexes and aggregation functions 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)
17 views

Rebellabs SQL Cheat Sheeasdasdasd2222222t

The document provides an overview of basic SQL queries including selecting, filtering, aggregating, ordering and limiting data as well as joining tables. It also covers updating, inserting and deleting data along with using views, indexes and aggregation functions 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 rows
WHERE col4 = 1 ANDAsaDsaDADD
cols= 2
-- aggregate the data
GROUP by ...
-- limit aggregated data
HAVING count(*) > 1
-- order
OROER of BY
thecol2
results LEFT OUTER JOIN - al! rows from table A /NNERJOIN -fetch the results that RIGHT OUTER JOIN - al! rows from table 8,
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
BETWEEN a AND b - limit the range, the values can be Updates odasdasdaSDn • ctions
numbers, text, or dates JOINed Queries -- convert strings to dates:
LIKE - pattern search within the column text TO_DATE (Oracle, PostgreSQL), STR_TO_DATE (MySQL)
You can use JOINs in your UPDATEs
IN (a, b, c) - check if the value is contained among given. -- return the first non-NULL argument:
UPDATE t1 SET a = 1
FROM table1 t1 JOIN table2 t2 ON t1.id = t2.t1_id COALESCE (col1, col2, "default value")
-- return current time:
Data Modification WHERE t1.col1 = 0 AND t2.col2 IS NULL;
CURRENT_TIMESTAMP
-- update specific data with the WHERE clause NBI Use database specific syntax, it might be faster! -- compute set operations on two result sets
UPDATE table1 SET col1 = 1 WHERE col2 = 2 SELECT col1, col2 FROM table1
-- insert values manually UNION/ EXCEPT/ INTERSECT
INSERT INTO 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