SQL Cheat Sheet, Summaries of Database Programming

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 ...

Typology: Summaries

2021/2022

Uploaded on 07/05/2022

paul.kc
paul.kc 🇦🇺

4.7

(68)

1K documents

1 / 11

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
SQL !
Cheat Sheet
Mosh Hamedani
Code with Mosh (codewithmosh.com)
1st Edition
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download SQL Cheat Sheet and more Summaries Database Programming in PDF only on Docsity!

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/

  • Basics
  • Comments
  • SELECT Clause
  • WHERE Clause
  • Logical Operators
  • IN Operator
  • BETWEEN Operator
  • LIKE Operator
  • REGEXP Operator
  • IS NULL Operator
  • ORDER BY Clause
  • LIMIT Clause
  • Inner Joins
  • Outer Joins
  • USING Clause
  • Cross Joins
  • Unions
  • Inserting Data
  • Want to Become a SQL Expert?

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

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

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/