0% found this document useful (0 votes)
100 views3 pages

SQL Basics Cheat Sheet Guide

Uploaded by

muraliinfo24
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
100 views3 pages

SQL Basics Cheat Sheet Guide

Uploaded by

muraliinfo24
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

SQL Basics Cheat Sheet

What is SQL? The different dialects of SQL

Sample Data

airbnb_listings

id city country number_of_rooms year_listed

1 Paris France 5 2018

2 Tokyo Japan 2 2017

3 New York USA 2 2022

Querying tables E Get the listing id, city, ordered by th


number_of_rooms in descending order
FE Get all the columns from a table
SELECT id, city

SELECT *
FROM airbnb_listings

FROM airbnb_listings; ORDER BY number_of_rooms DESC;

mE Return the city column from the table !E Get the first 5 rows from the
SELECT city
airbnb_listings table
FROM airbnb_listings; SELECT *

FROM airbnb_listings

’E Get the city and year_listed columns FROM 5;


from the table
SELECT city, year_listed
9E Get a unique list of cities where there are
FROM airbnb_listings; listings
*Source: Internet

SELECT city

ÂE Get the listing id, city, ordered by the FROM airbnb_listings;


number_of_rooms in ascending order
SELECT id, city

FROM airbnb_listings

ORDER BY number_of_rooms ASC;


SQL Basics Cheat Sheet
Filtering Data Filtering on text columns
Filtering on numeric columns ! Get all the listings that are based in ‘Paris’
SELECT *

-! Get all the listings where number_of_rooms FROM airbnb_listings

is more or equal to 3 WHERE city = ‘Paris’;


SELECT *

FROM airbnb_listings
=! Get all the listings based in the ‘USA’ and in
WHERE number_of_rooms >= 3; ‘France’
SELECT *

j! Get all the listings where number_of_rooms FROM airbnb_listings

is more than 3 WHERE country IN (‘USA’, ‘France’);


SELECT *

FROM airbnb_listings
! Get all the listings where the city starts with
WHERE number_of_rooms > 3; ‘j’ and where the city does not end in ‘t’
SELECT *

€! Get all the listings where number_of_rooms FROM airbnb_listings

is exactly equal to 3 WHERE city LIKE ‘j%’ AND city NOT


SELECT *
LIKE ‘%t’;
FROM airbnb_listings

WHERE number_of_rooms = 3; Filtering on multiple columns


“! Get all the listings where number_of_rooms -É! Get all the listings in ‘Paris’ where
is lower or equal to 3 number_of_rooms is bigger than 3
SELECT *
SELECT *

FROM airbnb_listings
FROM airbnb_listings

WHERE number_of_rooms <= 3; WHERE city = ‘Paris’; AND


number_of_rooms > 3;
¬! Get all the listings where number_of_rooms
is lower than 3 --! Get all the listings in ‘Paris’ OR the ones
SELECT *
that were listed after 2012
FROM airbnb_listings
SELECT *

WHERE number_of_rooms < 3; FROM airbnb_listings

WHERE city = ‘Paris’; OR year_listed > 2012;


È! Get all the listings with 3 to 6 rooms
SELECT *
Filtering on missing data
FROM airbnb_listings

WHERE number_of_rooms BETWEEN 3 AND 6;


*Source: Internet

-j! Return the listings where number_of_rooms


is missing
SELECT *

FROM airbnb_listings

WHERE number_of_rooms IS NULL;


SQL Basics Cheat Sheet
 Return the listings where number_of_rooms is + Get the listing with the maximum number of
not missing rooms per country
SELECT *
SELECT country, MAX(number_of_rooms)

FROM airbnb_listings
FROM airbnb_listings

WHERE number_of_rooms IS NOT NULL; GROUP BY country;

AGGREGATING DATA A Get the listing with the lowest amount of


rooms per country
Simple aggregations SELECT country, MIN(number_of_rooms)

FROM airbnb_listings

 Get the total number of rooms available across GROUP BY country;


all listings
Z For each country, get the average number of
SELECT SUM(number_of_rooms)
rooms per listing, sorted by ascending order
FROM airbnb_listings;
SELECT country, AVG(number_of_rooms)
Œ Get the average number of rooms per listing AS avg_rooms

across all listings FROM airbnb_listings

GROUP BY country;

SELECT AVG(number_of_rooms)
ORDER BY avg_rooms ASC;
FROM airbnb_listings;
“ For Japan and the USA, get the average
 Get the listing with the highest number of rooms number of rooms per listing in each country
across all listings
SELECT country, MAX(number_of_rooms)

SELECT MAX(number_of_rooms)
FROM airbnb_listings

FROM airbnb_listings; WHERE country IN (‘USA’, ‘Japan’);

GROUP BY country;
§ Get the listing with the lowest number of rooms
across all listings  Get the number of cities per country, where
SELECT MIN(number_of_rooms)
there are listings
FROM airbnb_listings; SELECT country, COUNT(city) AS
number_of_cities

Grouping, filtering, and sorting FROM airbnb_listings

GROUP BY country;
Ë Get the total number of rooms for each country
Œ Get all the years where there were more than
SELECT country, SUM(number_of_rooms)
100 listings per year
FROM airbnb_listings

GROUP BY country; SELECT year_listed

FROM airbnb_listings

*Source: Internet

 Get the average number of rooms for each country GROUP BY year_listed

HAVING COUNT(id) > 100;


SELECT country, AVG(number_of_rooms)

FROM airbnb_listings

GROUP BY country;

Common questions

Powered by AI

SQL filtering can be done using various operators suited for different needs. For numeric columns, operators like '>=' (greater than or equal to), '>' (greater than), '=' (equal to), '<=' (less than or equal to), '<' (less than), and 'BETWEEN ... AND ...' (range) allow for precise numeric data queries . For text columns, operators like '=' (equals), 'IN' (within a set), and 'LIKE' (pattern matches) are used. An example is IN which filters entries based on multiple values, such as listing all in 'USA' or 'France' . Conceptually, numeric filters tend to be straightforward comparisons while text filtering might involve pattern matching for flexibility in queries.

SQL achieves data validation and integrity through constraints and triggers. Constraints like PRIMARY KEY, UNIQUE, NOT NULL, and CHECK enforce rules at the schema level, ensuring data correctness and consistency by restricting invalid data entry (e.g., ensuring all entries in a required column must have a value with NOT NULL). Triggers can automate actions such as logging changes, enforcing complex validation rules, or updating related records, maintaining integrity across complex interactions. These mechanisms help avoid data corruption and ensure reliable data states throughout database operations.

SQL primarily handles aggregations like COUNT or DISTINCT for non-numeric data, assessing quantities or unique entries rather than calculating mathematical aggregates like SUM or AVG . For example, counting cities per country (SELECT COUNT(city)) determines the number of distinct locations within each category . However, the limitation is that such aggregation doesn't provide additive insights—such as a sum or average—limiting the types of summaries that can be derived, often necessitating conversion or alternative approaches when deeper statistical analysis is required.

The WHERE clause in SQL adapts to the data type of the column being queried. For integer or decimal types, numeric comparisons (e.g., WHERE number_of_rooms > 3) filter based on numerical value thresholds . For text data, LIKE is often used for pattern matching (e.g., WHERE city LIKE 'P%'), or exact matches (e.g., WHERE city = 'Paris'). Boolean operations can combine conditions across columns, such as filtering for records in 'Paris' where rooms are more than three, using multiple conditionals . These variegated uses demonstrate SQL's versatility in handling diverse datasets.

SQL can perform aggregations using functions like SUM, AVG, COUNT, MAX, and MIN to compile data into summary statistics. Aggregation operations are crucial for data analysis as they enable summarization of large datasets, highlighting overall trends and insights . For example, SUM(number_of_rooms) calculates total rooms across all listings, whereas AVG(number_of_rooms) gives the average, each providing a different perspective on the data . These operations help in reducing complexity and identifying patterns that inform decision-making processes.

Missing data in SQL is represented by NULL values which signify the absence of any data in a particular field. Queries can be affected by NULL values as they are often excluded in direct comparisons or calculations, potentially skewing results . To manage this, SQL provides functions like IS NULL and IS NOT NULL to identify missing data . Strategies to handle missing data include using COALESCE to provide default values or filtering said records to mitigate their influence on aggregates and calculations, ensuring accurate data processing and analysis.

SQL dialects, such as MySQL, PostgreSQL, and SQL Server, are variations of the SQL language tailored to different database systems, each featuring unique syntax extensions and capabilities. While the core SQL functionalities such as SELECT, INSERT, UPDATE, DELETE, and WHERE clauses remain consistent, each dialect may offer proprietary functions or optimizations specific to its architecture . For example, PostgreSQL offers extensive JOIN operations, and MySQL includes specific string functions not found in others. These differences require developers to tailor queries for cross-platform compatibility or optimize them for specific database systems.

In SQL, ASC (ascending) and DESC (descending) are used to sort data. ASC sorts records from the smallest to the largest, useful for ordering IDs or dates chronologically . DESC reverses this order, displaying records from largest to smallest, ideal for ranking data by highest value first such as listings with the most rooms . Strategically, ASC is used when natural, chronological, or alphabetical order is necessary, while DESC is applied when highlighting top performances or recent additions.

Advanced techniques for optimizing SQL queries include indexing columns to speed up search and join operations, using EXPLAIN to understand the query execution plan, and optimizing WHERE clauses by avoiding functions on indexed columns to maintain index usability. Additionally, breaking down complex queries into simpler sub-queries or using JOINs over sub-queries can enhance performance . Query restructuring, such as selecting only necessary fields rather than using SELECT *, and using proper indexing and normalization, can significantly reduce execution time and resource consumption.

Grouping data in SQL, achieved via GROUP BY, segments data into subsets, allowing for more detailed analysis and insights. It is particularly useful in conjunction with aggregation functions to obtain summary statistics for each group . For instance, calculating the sum of rooms by country (SELECT SUM(number_of_rooms) FROM airbnb_listings GROUP BY country) helps identify which countries have higher listing capacities, enhancing insights into geographic distribution of data . This method facilitates targeted analysis, turning raw data into actionable information.

You might also like