SQL Guide
SQL Guide
If you’re just getting started, it’s hard to know what exactly to learn in SQL.
At SQLPlay, we’ve tried to make your experience a tad bit easier with a roadmap.
When you’re building for the real world, we get a chance to visualize SQL.
In this ramp-up guide, you’ll learn SQL with the objective of creating your own Recipe
Database using the 15 most important SQL commands:
SQL Where
SQL Order By
SQL Between
SQL Like
SQL Group By
SQL Delete
15 Top SQL Commands for Beginners - A Ramp-Up Guide to Learn SQL Fast 1
SQL Having
SQL Joins
What is SQL?
Think of SQL database as a container for storing different items — your clothes, cutlery,
currency, computer.
Row → Record of an item (all details about a particular item, eg. its id, name and
quantity)
Column → Field or Attribute of items (item_name describes what kind of items you’re
storing in your table, eg. is it a jacket, socks, or graphic tees?)
id item_name quantity
1 Jacket 4
2 Socks 2
3 Graphic Tees 15
This is the kind of data you can store, and also intelligently retrieve.
Let’s see how to use SQL!
15 Top SQL Commands for Beginners - A Ramp-Up Guide to Learn SQL Fast 2
Why to use SQL or a Database?
Why not store data in text files?
Why even use a database?
searchable
sortable
constrained
structured
Imagine a bank employee going through all the paper records to find out how much
balance you have in your bank account.
bank-employee-smiling-and-checking-bank-balance
It is so tedious that every time you go and ask them, you wish you didn’t.
Having a database helps the bank employee to find out your bank balance in the
fraction of a second, using just your account number and running a query on SQL.
Just that, your banker doesn’t write SQL, the developers and DBAs
(database administrator) do.
15 Top SQL Commands for Beginners - A Ramp-Up Guide to Learn SQL Fast 3
Let’s not talk about systems that need big management.
Let’s talk about simpler systems that make us happy — food recipes.
We need to know what ingredients to use, in the right order and the time needed to
cook the dish.
To create a table that you can store your recipes in, run this SQL query:
⚠ For SQLite: replace SERIAL PRIMARY KEY with INTEGER PRIMARY KEY AUTOINCREMENT .
SQL needs instructions so here we tell it to create a table using CREATE TABLE command.
Now we need to our SQL table a name right after this command, example: recipes.
As we learnt earlier, SQL is all about structuring.
So we define what to store and what kind they are.
Not Null - If you define a field as not null, it can’t have empty values.
Serial - Works just like autoincrement but has built-in data type of BIGINT
15 Top SQL Commands for Beginners - A Ramp-Up Guide to Learn SQL Fast 4
Let’s learn about data types
Integer - Just the usual numbers. Eg. 1, 22, 66
String - You can put multiple words and phrases. Eg. 'Bakery' , 'https://sqlplay.net'
Varchar - Just like string but we limit with VARCHAR(n) , where n is the number of
characters.
table-with-empty-plate-and-cutllery
To insert your newfound recipes into the SQL table, run this command.
15 Top SQL Commands for Beginners - A Ramp-Up Guide to Learn SQL Fast 5
INSERT INTO recipes
VALUES (2, 'Meat Bolognese',
'Perfect, cheesy meat and eggs for the soul', 4, 40)
Note that this method of inserting into SQL table needs all column
values in order, including the painstaking ID.
1, 2, or 3, what do I know?
To avoid putting in IDs, field values in exact order every time, we can use the syntax
which specifies the field names:
15 Top SQL Commands for Beginners - A Ramp-Up Guide to Learn SQL Fast 6
3. How to get data from table — Select SQL rows
We just ran our fresh SQL INSERT command.
But, where is our data?
How do we verify that our data is safe in the table? Psst, it’s not!
Here SELECT does the job of, well, selecting or gathering items.
By using * , you select every column.
FROM recipes lets you select which table to view data from.
You can also change the column name and put a shorter alias using the keyword as
title detail
Spaghetti with Meatballs Perfect pasta for busy weeknights with juicy meatballs
15 Top SQL Commands for Beginners - A Ramp-Up Guide to Learn SQL Fast 7
filter-coffee-to-filter-sql-rows
You wake up but don’t accept how 5 minutes of snooze turned into an hour.
No time for a shower.
Meeting at 9 AM.
15 Top SQL Commands for Beginners - A Ramp-Up Guide to Learn SQL Fast 8
This ensures you get a recipe using SQL WHERE that takes lesser than (<) or equal to (=)
20 minutes.
20 minutes.
No more.
To get the recipes which serves at least two people, try on this SQL query:
With servings >= 2, you can ensure enough food for two.
You can use ORDER BY command with ASC for ascending (lowest to highest) or DESC
15 Top SQL Commands for Beginners - A Ramp-Up Guide to Learn SQL Fast 9
Today, search for everything whose description includes pasta.
Your job had you chained to a chair, not to the radius of a supermarket.
Now all you have in your pantry is … pasta.
Find a comforting recipe that’ll help you get some dog food today.
app% will select the rows which begin with app, like apple, apps, apply
%ap will select the rows which end with ap, like map, lap, cap.
%uck% will select the rows with uck in between, like lucky, fire trucks, chuckle.
f%y will select the rows which begin with f and ends with y, like firefly, fishy, fairy.
15 Top SQL Commands for Beginners - A Ramp-Up Guide to Learn SQL Fast 10
SELECT * from recipes WHERE cook_time LIKE '4_';
This will give us all the recipes which has cook time of 40 to 49 minutes.
It’s similar to SQL Between command in this use case.
You were quite busy with your work, so you asked your son to enter some recipes for
you.
Accidentally he misspelled spaghetti as spaghetty in some instances, so you’re pulling
your hair out trying to find the recipes.
Use the SQL NOT operator to filter out all the recipes which have ‘meat’ in their
description.
SQL AND
You decide to go vegan.
Again, not for the dog.
15 Top SQL Commands for Beginners - A Ramp-Up Guide to Learn SQL Fast 11
Use the SQL AND operator to filter out multiple recipes which contain even the slightest
amount of ‘meat’ and ‘egg’.
SELECT * from recipes WHERE NOT description LIKE '%meat%' AND '%egg%';
SELECT * from recipes WHERE NOT description LIKE '%meat%' AND description LIKE '%pasta%';
SQL OR
You’re vegan.
Your family isn’t.
15 Top SQL Commands for Beginners - A Ramp-Up Guide to Learn SQL Fast 12
This is the output which you’ll see:
Perfect, cheesy
2 Meat Bolognese meat and eggs for 2 40
the soul
If you’re not too sure, the SQL NOT operator comes in handy and dandy.
Avoid all meat recipes with this clever SQL NOT hack:
9. SQL Between
It’s Thanksgiving.
Nothing’s changed.
Just this time, you’re inviting your parents over.
15 Top SQL Commands for Beginners - A Ramp-Up Guide to Learn SQL Fast 13
The turkey’s warm - sitting in the oven. But not for long.
Your parents will be home in 2 hours.
Find a recipe that will help you quickly set the table — with the big turkey as the main
dish.
This will give you all the recipes which have cook time ranging from 20 to 60. No more,
no less.
But then, you had a new finding — maybe the turkey could use more time in the oven.
Use the SQL Update command to update your cook time findings in the recipe:
UPDATE recipes
SET cook_time=60
WHERE title LIKE '%Turkey%'
15 Top SQL Commands for Beginners - A Ramp-Up Guide to Learn SQL Fast 14
With the SET command you can set the field value to an exact value.
Example: Set the cook_time to be 60 minutes.
⚠ If you don’t put a SQL WHERE clause, your query will update every single
row of your table.
Here’s your SQL Delete command that’ll take care of that for you:
With SQL Count command, you will know the exact number of items in your table. Run
this:
Want to know what’s the average cook time for all your recipes? Use the SQL AVG
command:
15 Top SQL Commands for Beginners - A Ramp-Up Guide to Learn SQL Fast 15
13. Join Multiple Tables on Common Row — SQL Joins
Right now your SQL table for recipes only has the title, description, cook time and
servings.
It doesn’t tell you about the ingredients to be used in order and the quantity of each
ingredient.
⚠ For SQLite: replace SERIAL PRIMARY KEY with INTEGER PRIMARY KEY AUTOINCREMENT .
Notice that recipe_id here is the common key between both our tables recipes and
ingredients. This is called a foreign key — a link to join two tables in SQL.
15 Top SQL Commands for Beginners - A Ramp-Up Guide to Learn SQL Fast 16
Insert rows using SQL with these commands:
15 Top SQL Commands for Beginners - A Ramp-Up Guide to Learn SQL Fast 17
INSERT INTO ingredients
VALUES (6, 4, 'eggs', 5);
1 1 spaghetti 200
2 1 tomato puree 50
3 1 white onions 20
4 1 salt 4
5 1 feta cheese 30
The field recipe_id lets you know which recipe these ingredients are from.
Let’s perform the simplest inner join on these two tables — recipes and ingredients.
SELECT *
FROM recipes
INNER JOIN ingredients ON
ingredients.recipe_id = recipes.id
In SQL Inner Join returns all rows from both tables where there is a match.
15 Top SQL Commands for Beginners - A Ramp-Up Guide to Learn SQL Fast 18
1 1 spaghetti 200 1 Sp
Me
Sp
2 1 tomato puree 50 1
Me
Sp
3 1 white onions 20 1
Me
Sp
4 1 salt 4 1
Me
Sp
5 1 feta cheese 30 1
Me
You got all the ingredients needed for your spaghetti recipe in a sweet, simple SQL
table!
To-do lists, where you at?
15 Top SQL Commands for Beginners - A Ramp-Up Guide to Learn SQL Fast 19
You would want to know how many ingredients you need, and their total quantity you
need for all that’s on your menu.
You can use AS like an alias for your columns to make it look pretty.
white onions 2 50
salt 2 12
tomato puree 1 50
spaghetti 1 200
15 Top SQL Commands for Beginners - A Ramp-Up Guide to Learn SQL Fast 20
garlic 1 2
galangal 1 1
feta cheese 1 30
eggs 1 5
Output of query:
salt 2 12
Where To Go Next?
When you’re working with a database, there’s all sorts of operations you can do to make
your database management journey a bit easier.
the rebels — SQL Where, SQL AND, OR, NOT, SQL Update
the misfits — SQL Order By, SQL Not Equal, SQL Delete, SQL Count, Avg, Sum
the troublemakers — SQL Like, SQL Joins, SQL Between, SQL Group By, SQL
Having
The SQL Having clause with the SQL Group By ones, they’re not fond of SQL ORDER
BYs.
They have no respect for SQL key constraints.
15 Top SQL Commands for Beginners - A Ramp-Up Guide to Learn SQL Fast 21
You can use them for your queries, disagree with the outputs and shake your head, or
glorify them after correcting your query.
The only thing you can't do is ignore them.
While some may see them as the crazy SQL commands, we see
genius:
Genius enough to achieve that employee raise calculation, and all together, the SQL
commands that can change your life too.
If you’re that genius the world is looking for, don’t waste your time setting up a huge
MySQL, SQLite or PostgreSQL database on your computer.
Get started on creating those magical SQL queries that can change your world by
getting the SQL Play app that’s ready for your genius.
15 Top SQL Commands for Beginners - A Ramp-Up Guide to Learn SQL Fast 22