SlideShare a Scribd company logo
SQL 

Cheat Sheet
Mosh Hamedani
Code with Mosh (codewithmosh.com)
1st Edition
About this Cheat Sheet
This cheat sheet includes the materials I’ve covered in my SQL tutorial for
Beginners on YouTube.
https://youtu.be/7S_tz1z_5bA
Both the YouTube tutorial and this cheat cover the core language constructs and
they are not complete by any means.
If you want to learn everything SQL has to offer and become a SQL expert, check
out my Complete SQL Mastery Course.
Use the coupon code CHEATSHEET upon checkout to get this course with a
90% discount:
https://codewithmosh.com/p/complete-sql-mastery/
About the Author


Hi! My name is Mosh Hamedani. I’m a software engineer
with two decades of experience and I’ve taught over three
million how to code or how to become a professional
software engineer. It’s my mission to make software 

engineering simple and accessible to everyone.
https://codewithmosh.com
https://youtube.com/user/programmingwithmosh
https://twitter.com/moshhamedani
https://facebook.com/programmingwithmosh/
Basics 5........................................................................................................
Comments 5.................................................................................................
SELECT Clause 5........................................................................................
WHERE Clause 6........................................................................................
Logical Operators 6....................................................................................
IN Operator 7..............................................................................................
BETWEEN Operator 7................................................................................
LIKE Operator 7..........................................................................................
REGEXP Operator 7....................................................................................
IS NULL Operator 8...................................................................................
ORDER BY Clause 8...................................................................................
LIMIT Clause 8............................................................................................
Inner Joins 9...............................................................................................
Outer Joins 9...............................................................................................
USING Clause 9...........................................................................................
Cross Joins 9................................................................................................
Unions 10.....................................................................................................
Inserting Data 10........................................................................................
Want to Become a SQL Expert? 10............................................................
Basics
USE sql_store;
SELECT *

FROM customers 

WHERE state = ‘CA’

ORDER BY first_name

LIMIT 3;
• SQL is not a case-sensitive language.
• In MySQL, every statement must be terminated with a semicolon.
Comments
We use comments to add notes to our code.
—- This is a comment and it won’t get executed.

SELECT Clause
—- Using expressions
SELECT (points * 10 + 20) AS discount_factor

FROM customers
Order of operations:
• Parenthesis
• Multiplication / division
• Addition / subtraction


—- Removing duplicates
SELECT DISTINCT state

FROM customers 

WHERE Clause
We use the WHERE clause to filter data.


Comparison operators:
• Greater than: >
• Greater than or equal to: >=
• Less than: <
• Less than or equal to: <=
• Equal: =
• Not equal: <>
• Not equal: !=
Logical Operators


—- AND (both conditions must be True) 

SELECT *

FROM customers 

WHERE birthdate > ‘1990-01-01’ AND points > 1000 

—- OR (at least one condition must be True) 

SELECT *

FROM customers 

WHERE birthdate > ‘1990-01-01’ OR points > 1000 



—- NOT (to negate a condition) 

SELECT *

FROM customers 

WHERE NOT (birthdate > ‘1990-01-01’)
IN Operator
—- Returns customers in any of these states: VA, NY, CA

SELECT *

FROM customers 

WHERE state IN (‘VA’, ‘NY’, ‘CA’)
BETWEEN Operator
SELECT *

FROM customers 

WHERE points BETWEEN 100 AND 200
LIKE Operator
—- Returns customers whose first name starts with b 

SELECT *

FROM customers 

WHERE first_name LIKE ‘b%’
• %: any number of characters
• _: exactly one character
REGEXP Operator
—- Returns customers whose first name starts with a 

SELECT *

FROM customers 

WHERE first_name REGEXP ‘^a’
• ^: beginning of a string
• $: end of a string
• |: logical OR
• [abc]: match any single characters
• [a-d]: any characters from a to d
More Examples 

—- Returns customers whose first name ends with EY or ON 

WHERE first_name REGEXP ‘ey$|on$’
—- Returns customers whose first name starts with MY 

—- or contains SE

WHERE first_name REGEXP ‘^my|se’
—- Returns customers whose first name contains B followed by 

—- R or U

WHERE first_name REGEXP ‘b[ru]’
IS NULL Operator
—- Returns customers who don’t have a phone number 

SELECT *

FROM customers 

WHERE phone IS NULL
ORDER BY Clause
—- Sort customers by state (in ascending order), and then 

—- by their first name (in descending order) 

SELECT *

FROM customers 

ORDER BY state, first_name DESC
LIMIT Clause
—- Return only 3 customers 

SELECT *

FROM customers 

LIMIT 3
—- Skip 6 customers and return 3

SELECT *

FROM customers 

LIMIT 6, 3
Inner Joins
SELECT *

FROM customers c

JOIN orders o 

ON c.customer_id = o.customer_id
Outer Joins
—- Return all customers whether they have any orders or not

SELECT *

FROM customers c

LEFT JOIN orders o 

ON c.customer_id = o.customer_id
USING Clause
If column names are exactly the same, you can simplify the join with the USING
clause.
SELECT *

FROM customers c

JOIN orders o 

USING (customer_id)
Cross Joins
—- Combine every color with every size

SELECT *

FROM colors 

CROSS JOIN sizes
Unions
—- Combine records from multiple result sets

SELECT name, address

FROM customers 

UNION

SELECT name, address

FROM clients 

Inserting Data
—- Insert a single record

INSERT INTO customers(first_name, phone, points)

VALUES (‘Mosh’, NULL, DEFAULT)
—- Insert multiple single records

INSERT INTO customers(first_name, phone, points)

VALUES 

(‘Mosh’, NULL, DEFAULT),

(‘Bob’, ‘1234’, 10)



Want to Become a SQL Expert?
If you’re serious about learning SQL and getting a job as a software developer or
data scientist, I highly encourage you to enroll in my Complete SQL Mastery
Course. Don’t waste your time following disconnected, outdated tutorials. My
Complete SQL Mastery Course has everything you need in one place:
• 10 hours of HD video
• Unlimited access - watch it as many times as you want
• Self-paced learning - take your time if you prefer
• Watch it online or download and watch offline
• Certificate of completion - add it to your resume to stand out
• 30-day money-back guarantee - no questions asked
The price for this course is $149 but the first 200 people who have downloaded this
cheat sheet can get it for $12.99 using the coupon code CHEATSHEET:
https://codewithmosh.com/p/complete-sql-mastery/

More Related Content

Similar to SQL sheet

Beginers guide for oracle sql
Beginers guide for oracle sqlBeginers guide for oracle sql
Beginers guide for oracle sql
N.Jagadish Kumar
 
Module 3.1.pptx
Module 3.1.pptxModule 3.1.pptx
Module 3.1.pptx
ANSHVAJPAI
 
Unit 3-Select Options and Aggregate Functions in SQL (1).pptx
Unit 3-Select Options and Aggregate Functions in SQL (1).pptxUnit 3-Select Options and Aggregate Functions in SQL (1).pptx
Unit 3-Select Options and Aggregate Functions in SQL (1).pptx
HAMEEDHUSSAINBU21CSE
 
Dynamic websites lec2
Dynamic websites lec2Dynamic websites lec2
Dynamic websites lec2
Belal Arfa
 
Chinabankppt
ChinabankpptChinabankppt
Chinabankppt
newrforce
 
Introduction to SQL
Introduction to SQLIntroduction to SQL
Introduction to SQL
Mahir Haque
 
SQL Lesson 6 - Select.pdf
SQL Lesson 6 - Select.pdfSQL Lesson 6 - Select.pdf
SQL Lesson 6 - Select.pdf
Madhusha15
 
75864 sql
75864 sql75864 sql
75864 sql
bansalaman80
 
1 z1 051
1 z1 0511 z1 051
1 z1 051
AHMED ENNAJI
 
Complete Sql Server querries
Complete Sql Server querriesComplete Sql Server querries
Complete Sql Server querries
Ibrahim Jutt
 
Presentation top tips for getting optimal sql execution
Presentation    top tips for getting optimal sql executionPresentation    top tips for getting optimal sql execution
Presentation top tips for getting optimal sql execution
xKinAnx
 
Aggregate Functions,Final
Aggregate Functions,FinalAggregate Functions,Final
Aggregate Functions,Final
mukesh24pandey
 
Oracle OCP 1Z0-007题库
Oracle OCP 1Z0-007题库Oracle OCP 1Z0-007题库
Oracle OCP 1Z0-007题库
renguzi
 
The ultimate-guide-to-sql
The ultimate-guide-to-sqlThe ultimate-guide-to-sql
The ultimate-guide-to-sql
McNamaraChiwaye
 
Build .NET Applications with Reporting and Dashboard
Build .NET Applications with Reporting and DashboardBuild .NET Applications with Reporting and Dashboard
Build .NET Applications with Reporting and Dashboard
Iron Speed
 
Les02 (restricting and sorting data)
Les02 (restricting and sorting data)Les02 (restricting and sorting data)
Les02 (restricting and sorting data)
Achmad Solichin
 
Les02
Les02Les02
Sql wksht-6
Sql wksht-6Sql wksht-6
Sql wksht-6
Mukesh Tekwani
 
DB_lecturs8 27 11.pptx
DB_lecturs8 27 11.pptxDB_lecturs8 27 11.pptx
DB_lecturs8 27 11.pptx
NermeenKamel7
 
SQL- Introduction to MySQL
SQL- Introduction to MySQLSQL- Introduction to MySQL
SQL- Introduction to MySQL
Vibrant Technologies & Computers
 

Similar to SQL sheet (20)

Beginers guide for oracle sql
Beginers guide for oracle sqlBeginers guide for oracle sql
Beginers guide for oracle sql
 
Module 3.1.pptx
Module 3.1.pptxModule 3.1.pptx
Module 3.1.pptx
 
Unit 3-Select Options and Aggregate Functions in SQL (1).pptx
Unit 3-Select Options and Aggregate Functions in SQL (1).pptxUnit 3-Select Options and Aggregate Functions in SQL (1).pptx
Unit 3-Select Options and Aggregate Functions in SQL (1).pptx
 
Dynamic websites lec2
Dynamic websites lec2Dynamic websites lec2
Dynamic websites lec2
 
Chinabankppt
ChinabankpptChinabankppt
Chinabankppt
 
Introduction to SQL
Introduction to SQLIntroduction to SQL
Introduction to SQL
 
SQL Lesson 6 - Select.pdf
SQL Lesson 6 - Select.pdfSQL Lesson 6 - Select.pdf
SQL Lesson 6 - Select.pdf
 
75864 sql
75864 sql75864 sql
75864 sql
 
1 z1 051
1 z1 0511 z1 051
1 z1 051
 
Complete Sql Server querries
Complete Sql Server querriesComplete Sql Server querries
Complete Sql Server querries
 
Presentation top tips for getting optimal sql execution
Presentation    top tips for getting optimal sql executionPresentation    top tips for getting optimal sql execution
Presentation top tips for getting optimal sql execution
 
Aggregate Functions,Final
Aggregate Functions,FinalAggregate Functions,Final
Aggregate Functions,Final
 
Oracle OCP 1Z0-007题库
Oracle OCP 1Z0-007题库Oracle OCP 1Z0-007题库
Oracle OCP 1Z0-007题库
 
The ultimate-guide-to-sql
The ultimate-guide-to-sqlThe ultimate-guide-to-sql
The ultimate-guide-to-sql
 
Build .NET Applications with Reporting and Dashboard
Build .NET Applications with Reporting and DashboardBuild .NET Applications with Reporting and Dashboard
Build .NET Applications with Reporting and Dashboard
 
Les02 (restricting and sorting data)
Les02 (restricting and sorting data)Les02 (restricting and sorting data)
Les02 (restricting and sorting data)
 
Les02
Les02Les02
Les02
 
Sql wksht-6
Sql wksht-6Sql wksht-6
Sql wksht-6
 
DB_lecturs8 27 11.pptx
DB_lecturs8 27 11.pptxDB_lecturs8 27 11.pptx
DB_lecturs8 27 11.pptx
 
SQL- Introduction to MySQL
SQL- Introduction to MySQLSQL- Introduction to MySQL
SQL- Introduction to MySQL
 

Recently uploaded

Chapter wise All Notes of First year Basic Civil Engineering.pptx
Chapter wise All Notes of First year Basic Civil Engineering.pptxChapter wise All Notes of First year Basic Civil Engineering.pptx
Chapter wise All Notes of First year Basic Civil Engineering.pptx
Denish Jangid
 
Présentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
Présentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptxPrésentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
Présentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
siemaillard
 
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
PECB
 
The basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptxThe basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptx
heathfieldcps1
 
Constructing Your Course Container for Effective Communication
Constructing Your Course Container for Effective CommunicationConstructing Your Course Container for Effective Communication
Constructing Your Course Container for Effective Communication
Chevonnese Chevers Whyte, MBA, B.Sc.
 
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdfANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
Priyankaranawat4
 
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptxC1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
mulvey2
 
BBR 2024 Summer Sessions Interview Training
BBR  2024 Summer Sessions Interview TrainingBBR  2024 Summer Sessions Interview Training
BBR 2024 Summer Sessions Interview Training
Katrina Pritchard
 
spot a liar (Haiqa 146).pptx Technical writhing and presentation skills
spot a liar (Haiqa 146).pptx Technical writhing and presentation skillsspot a liar (Haiqa 146).pptx Technical writhing and presentation skills
spot a liar (Haiqa 146).pptx Technical writhing and presentation skills
haiqairshad
 
Solutons Maths Escape Room Spatial .pptx
Solutons Maths Escape Room Spatial .pptxSolutons Maths Escape Room Spatial .pptx
Solutons Maths Escape Room Spatial .pptx
spdendr
 
Hindi varnamala | hindi alphabet PPT.pdf
Hindi varnamala | hindi alphabet PPT.pdfHindi varnamala | hindi alphabet PPT.pdf
Hindi varnamala | hindi alphabet PPT.pdf
Dr. Mulla Adam Ali
 
A Independência da América Espanhola LAPBOOK.pdf
A Independência da América Espanhola LAPBOOK.pdfA Independência da América Espanhola LAPBOOK.pdf
A Independência da América Espanhola LAPBOOK.pdf
Jean Carlos Nunes Paixão
 
clinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdfclinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdf
Priyankaranawat4
 
Leveraging Generative AI to Drive Nonprofit Innovation
Leveraging Generative AI to Drive Nonprofit InnovationLeveraging Generative AI to Drive Nonprofit Innovation
Leveraging Generative AI to Drive Nonprofit Innovation
TechSoup
 
How to Make a Field Mandatory in Odoo 17
How to Make a Field Mandatory in Odoo 17How to Make a Field Mandatory in Odoo 17
How to Make a Field Mandatory in Odoo 17
Celine George
 
Pengantar Penggunaan Flutter - Dart programming language1.pptx
Pengantar Penggunaan Flutter - Dart programming language1.pptxPengantar Penggunaan Flutter - Dart programming language1.pptx
Pengantar Penggunaan Flutter - Dart programming language1.pptx
Fajar Baskoro
 
How to deliver Powerpoint Presentations.pptx
How to deliver Powerpoint  Presentations.pptxHow to deliver Powerpoint  Presentations.pptx
How to deliver Powerpoint Presentations.pptx
HajraNaeem15
 
Gender and Mental Health - Counselling and Family Therapy Applications and In...
Gender and Mental Health - Counselling and Family Therapy Applications and In...Gender and Mental Health - Counselling and Family Therapy Applications and In...
Gender and Mental Health - Counselling and Family Therapy Applications and In...
PsychoTech Services
 
BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 9 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2024-2025 - ...
BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 9 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2024-2025 - ...BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 9 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2024-2025 - ...
BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 9 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2024-2025 - ...
Nguyen Thanh Tu Collection
 
Advanced Java[Extra Concepts, Not Difficult].docx
Advanced Java[Extra Concepts, Not Difficult].docxAdvanced Java[Extra Concepts, Not Difficult].docx
Advanced Java[Extra Concepts, Not Difficult].docx
adhitya5119
 

Recently uploaded (20)

Chapter wise All Notes of First year Basic Civil Engineering.pptx
Chapter wise All Notes of First year Basic Civil Engineering.pptxChapter wise All Notes of First year Basic Civil Engineering.pptx
Chapter wise All Notes of First year Basic Civil Engineering.pptx
 
Présentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
Présentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptxPrésentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
Présentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
 
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
 
The basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptxThe basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptx
 
Constructing Your Course Container for Effective Communication
Constructing Your Course Container for Effective CommunicationConstructing Your Course Container for Effective Communication
Constructing Your Course Container for Effective Communication
 
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdfANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
 
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptxC1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
 
BBR 2024 Summer Sessions Interview Training
BBR  2024 Summer Sessions Interview TrainingBBR  2024 Summer Sessions Interview Training
BBR 2024 Summer Sessions Interview Training
 
spot a liar (Haiqa 146).pptx Technical writhing and presentation skills
spot a liar (Haiqa 146).pptx Technical writhing and presentation skillsspot a liar (Haiqa 146).pptx Technical writhing and presentation skills
spot a liar (Haiqa 146).pptx Technical writhing and presentation skills
 
Solutons Maths Escape Room Spatial .pptx
Solutons Maths Escape Room Spatial .pptxSolutons Maths Escape Room Spatial .pptx
Solutons Maths Escape Room Spatial .pptx
 
Hindi varnamala | hindi alphabet PPT.pdf
Hindi varnamala | hindi alphabet PPT.pdfHindi varnamala | hindi alphabet PPT.pdf
Hindi varnamala | hindi alphabet PPT.pdf
 
A Independência da América Espanhola LAPBOOK.pdf
A Independência da América Espanhola LAPBOOK.pdfA Independência da América Espanhola LAPBOOK.pdf
A Independência da América Espanhola LAPBOOK.pdf
 
clinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdfclinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdf
 
Leveraging Generative AI to Drive Nonprofit Innovation
Leveraging Generative AI to Drive Nonprofit InnovationLeveraging Generative AI to Drive Nonprofit Innovation
Leveraging Generative AI to Drive Nonprofit Innovation
 
How to Make a Field Mandatory in Odoo 17
How to Make a Field Mandatory in Odoo 17How to Make a Field Mandatory in Odoo 17
How to Make a Field Mandatory in Odoo 17
 
Pengantar Penggunaan Flutter - Dart programming language1.pptx
Pengantar Penggunaan Flutter - Dart programming language1.pptxPengantar Penggunaan Flutter - Dart programming language1.pptx
Pengantar Penggunaan Flutter - Dart programming language1.pptx
 
How to deliver Powerpoint Presentations.pptx
How to deliver Powerpoint  Presentations.pptxHow to deliver Powerpoint  Presentations.pptx
How to deliver Powerpoint Presentations.pptx
 
Gender and Mental Health - Counselling and Family Therapy Applications and In...
Gender and Mental Health - Counselling and Family Therapy Applications and In...Gender and Mental Health - Counselling and Family Therapy Applications and In...
Gender and Mental Health - Counselling and Family Therapy Applications and In...
 
BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 9 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2024-2025 - ...
BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 9 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2024-2025 - ...BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 9 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2024-2025 - ...
BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 9 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2024-2025 - ...
 
Advanced Java[Extra Concepts, Not Difficult].docx
Advanced Java[Extra Concepts, Not Difficult].docxAdvanced Java[Extra Concepts, Not Difficult].docx
Advanced Java[Extra Concepts, Not Difficult].docx
 

SQL sheet

  • 1. SQL 
 Cheat Sheet Mosh Hamedani Code with Mosh (codewithmosh.com) 1st Edition
  • 2. About this Cheat Sheet This cheat sheet includes the materials I’ve covered in my SQL tutorial for Beginners on YouTube. https://youtu.be/7S_tz1z_5bA Both the YouTube tutorial and this cheat cover the core language constructs and they are not complete by any means. If you want to learn everything SQL has to offer and become a SQL expert, check out my Complete SQL Mastery Course. Use the coupon code CHEATSHEET upon checkout to get this course with a 90% discount: https://codewithmosh.com/p/complete-sql-mastery/
  • 3. About the Author 
 Hi! My name is Mosh Hamedani. I’m a software engineer with two decades of experience and I’ve taught over three million how to code or how to become a professional software engineer. It’s my mission to make software 
 engineering simple and accessible to everyone. https://codewithmosh.com https://youtube.com/user/programmingwithmosh https://twitter.com/moshhamedani https://facebook.com/programmingwithmosh/
  • 4. Basics 5........................................................................................................ Comments 5................................................................................................. SELECT Clause 5........................................................................................ WHERE Clause 6........................................................................................ Logical Operators 6.................................................................................... IN Operator 7.............................................................................................. BETWEEN Operator 7................................................................................ LIKE Operator 7.......................................................................................... REGEXP Operator 7.................................................................................... IS NULL Operator 8................................................................................... ORDER BY Clause 8................................................................................... LIMIT Clause 8............................................................................................ Inner Joins 9............................................................................................... Outer Joins 9............................................................................................... USING Clause 9........................................................................................... Cross Joins 9................................................................................................ Unions 10..................................................................................................... Inserting Data 10........................................................................................ Want to Become a SQL Expert? 10............................................................
  • 5. Basics USE sql_store; SELECT *
 FROM customers 
 WHERE state = ‘CA’
 ORDER BY first_name
 LIMIT 3; • SQL is not a case-sensitive language. • In MySQL, every statement must be terminated with a semicolon. Comments We use comments to add notes to our code. —- This is a comment and it won’t get executed.
 SELECT Clause —- Using expressions SELECT (points * 10 + 20) AS discount_factor
 FROM customers Order of operations: • Parenthesis • Multiplication / division • Addition / subtraction 
 —- Removing duplicates SELECT DISTINCT state
 FROM customers 

  • 6. WHERE Clause We use the WHERE clause to filter data. 
 Comparison operators: • Greater than: > • Greater than or equal to: >= • Less than: < • Less than or equal to: <= • Equal: = • Not equal: <> • Not equal: != Logical Operators 
 —- AND (both conditions must be True) 
 SELECT *
 FROM customers 
 WHERE birthdate > ‘1990-01-01’ AND points > 1000 
 —- OR (at least one condition must be True) 
 SELECT *
 FROM customers 
 WHERE birthdate > ‘1990-01-01’ OR points > 1000 
 
 —- NOT (to negate a condition) 
 SELECT *
 FROM customers 
 WHERE NOT (birthdate > ‘1990-01-01’)
  • 7. IN Operator —- Returns customers in any of these states: VA, NY, CA
 SELECT *
 FROM customers 
 WHERE state IN (‘VA’, ‘NY’, ‘CA’) BETWEEN Operator SELECT *
 FROM customers 
 WHERE points BETWEEN 100 AND 200 LIKE Operator —- Returns customers whose first name starts with b 
 SELECT *
 FROM customers 
 WHERE first_name LIKE ‘b%’ • %: any number of characters • _: exactly one character REGEXP Operator —- Returns customers whose first name starts with a 
 SELECT *
 FROM customers 
 WHERE first_name REGEXP ‘^a’ • ^: beginning of a string • $: end of a string • |: logical OR • [abc]: match any single characters • [a-d]: any characters from a to d
  • 8. More Examples 
 —- Returns customers whose first name ends with EY or ON 
 WHERE first_name REGEXP ‘ey$|on$’ —- Returns customers whose first name starts with MY 
 —- or contains SE
 WHERE first_name REGEXP ‘^my|se’ —- Returns customers whose first name contains B followed by 
 —- R or U
 WHERE first_name REGEXP ‘b[ru]’ IS NULL Operator —- Returns customers who don’t have a phone number 
 SELECT *
 FROM customers 
 WHERE phone IS NULL ORDER BY Clause —- Sort customers by state (in ascending order), and then 
 —- by their first name (in descending order) 
 SELECT *
 FROM customers 
 ORDER BY state, first_name DESC LIMIT Clause —- Return only 3 customers 
 SELECT *
 FROM customers 
 LIMIT 3
  • 9. —- Skip 6 customers and return 3
 SELECT *
 FROM customers 
 LIMIT 6, 3 Inner Joins SELECT *
 FROM customers c
 JOIN orders o 
 ON c.customer_id = o.customer_id Outer Joins —- Return all customers whether they have any orders or not
 SELECT *
 FROM customers c
 LEFT JOIN orders o 
 ON c.customer_id = o.customer_id USING Clause If column names are exactly the same, you can simplify the join with the USING clause. SELECT *
 FROM customers c
 JOIN orders o 
 USING (customer_id) Cross Joins —- Combine every color with every size
 SELECT *
 FROM colors 
 CROSS JOIN sizes
  • 10. Unions —- Combine records from multiple result sets
 SELECT name, address
 FROM customers 
 UNION
 SELECT name, address
 FROM clients 
 Inserting Data —- Insert a single record
 INSERT INTO customers(first_name, phone, points)
 VALUES (‘Mosh’, NULL, DEFAULT) —- Insert multiple single records
 INSERT INTO customers(first_name, phone, points)
 VALUES 
 (‘Mosh’, NULL, DEFAULT),
 (‘Bob’, ‘1234’, 10)
 
 Want to Become a SQL Expert? If you’re serious about learning SQL and getting a job as a software developer or data scientist, I highly encourage you to enroll in my Complete SQL Mastery Course. Don’t waste your time following disconnected, outdated tutorials. My Complete SQL Mastery Course has everything you need in one place: • 10 hours of HD video • Unlimited access - watch it as many times as you want • Self-paced learning - take your time if you prefer • Watch it online or download and watch offline • Certificate of completion - add it to your resume to stand out • 30-day money-back guarantee - no questions asked
  • 11. The price for this course is $149 but the first 200 people who have downloaded this cheat sheet can get it for $12.99 using the coupon code CHEATSHEET: https://codewithmosh.com/p/complete-sql-mastery/