You are on page 1of 10

www.tutort.

net

SQL Cheatsheet

Swipe

Machine Data Structures


Data Science System Design
Learning & Algorithms
www.tutort.net

querying single table

Fetch all columns from the country SELECT *

table: FROM country;

Fetch id and name columns from the SELECT id, name

city table: FROM city;

Fetch city names sorted by the SELECT name

rating column in the default FROM city


ASCending order:

Fetch city names sorted by the SELECT name

rating column in the DESCending FROM city

order: ORDER BY rating DESC;


www.tutort.net

aliases

COLUMNS SELECT name AS city_name

FROM city;

SELECT co.name, ci.name

TABLES FROM city AS ci

JOIN country AS co

ON ci.country_id = co.id;
www.tutort.net

filtering the output

COMPARISON OPERATORS
SELECT name

Fetch names of cities that have a rating FROM city

above 3: WHERE rating > 3;

SELECT name

Fetch names of cities that are


FROM city

neither Berlin nor Madrid:


WHERE name != 'Berlin'

AND name != 'Madrid';

TEXT OPERATORS
SELECT name

FROM city

Fetch names of cities that start with


a 'P' or end with an 's': WHERE name LIKE 'P%'

OR name LIKE '%s';

Fetch names of cities that start with SELECT name

any letter followed by 'ublin' (like FROM city

Dublin in Ireland or Lublin in Poland): WHERE name LIKE '_ublin';


www.tutort.net

OTHER OPERATORS
SELECT name

Fetch names of cities that have a FROM city

population between 500K and 5M: WHERE population BETWEEN


500000 AND 5000000;

SELECT name

Fetch names of cities that don't miss


FROM city

a rating value:
WHERE rating IS NOT NULL;

SELECT name

Fetch names of cities that are in FROM city

countries with IDs 1, 4, 7, or 8:


WHERE country_id IN (1, 4, 7, 8);
www.tutort.net

querying multiple tables

INNER JOIN

JOIN (or explicitly INNER JOIN) returns SELECT city.name, country.name

rows that have matching values in both FROM city

tables. [INNER] JOIN country

ON city.country_id = country.id;

LEFT JOIN

LEFT JOIN returns all rows from the


SELECT city.name, country.name

left table with corresponding rows


FROM city

from the right table. If there's no


matching row, NULLs are returned LEFT JOIN country

as values from the second table. ON city.country_id = country.id;


www.tutort.net

RIGHT JOIN

RIGHT JOIN returns all rows from the SELECT city.name, country.name

right table with corresponding rows FROM city

from the left table. If there's no RIGHT JOIN country

matching row, NULLs are returned as ON city.country_id = country.id;


values from the left table.

FULL JOIN

FULL JOIN (or explicitly FULL


SELECT city.name, country.name

OUTER JOIN) returns all rows from


FROM city

both tables – if there's no matching


row in the second table, NULLs are FULL [OUTER] JOIN country

returned. ON city.country_id = country.id;


www.tutort.net

aggregate functions

avg(expr) average value for rows within the


group

count(expr) count of values for rows within the


group

max(expr) maximum value within the group

min(expr) minimum value within the group

sum(expr) sum of values within the group


www.tutort.net

Expand your knowledge more!


Visit our YouTube Channel
Link in bio
www.tutort.net

Join Tutort Academy


153%
750+

Average Hike Students Places

17 LPA
93 LPA

Average CTC Highest Package

Visit our website for more

www.tutort.net

Save For later

You might also like