- Joined
- Apr 24, 2013
- Messages
- 1,684
- Reaction score
- 1,131
Hi!
Since all the SQL tutorials here seem to be very much outdated. I wanted to write a tutorial on how to write basic SQL statements. SQL is a VERY complex and nasty language, there's so much more to know than I can cover in a simple tutorial, but I will try my best.
I will start with a tutorial about selecting data, but if you guys want me to make more for updating, deleting, creating, subqueries, counting, summing or whatever, tell me.
Let's say we own a website which has users and these users can all update their profile with a certain country. Our tables look like this:
The most basic select statement looks like this:
Letās say we want to select the name and the id of all users, we would write it like this:
This will return the name and id of ALL the records. It will look like this:
But what if we only want to know the name and id from the users who live in Australia? We first need to look in our country table and we see that Australia has id 1. Now we have to add a āWHEREā statement to our SELECT. The most basic SELECT WHERE statement looks like this:
As you can see, you can add ORs and ANDs to your statement, this works just like you would expect it to work. If you are a programmer, you can see OR as || and AND as &&.
Now letās select our Australian users:
This will return only Australian users. It will look like this:
But what if we want to select id, name and country name? How do we do that? We can do that with a JOIN. There are a lot of different joins; INNER JOIN, OUTER JOIN, RIGHT JOIN, LEFT JOIN, RIGHT OUTER JOIN, etc.
This is a lot of information to take in at once, so weāll keep it simple and narrow this guide down to two JOINs: the LEFT JOIN and the INNER JOIN.
So what is the difference? Iāll show you this in the following examples, since writing it down is difficult.
The most basic JOIN looks like this:
The key columns represent a column belonging to table2 which is also present in table1.
Now to ALSO select the country name for all users, we write our SELECT statement like this:
We can add some aliases to make our query a bit more readable:
Using aliases is NOT a must, it just comes down to a matter of preference whether you want to use it or not.
However if you are selecting two columns with the same column name, in our example this is u.name and c.name, you have to add an alias to one of them. I would do it like this:
This query will return the following:
But wait.. Example Name5 hasnāt got a country_id filled in, what if we only want users who do have a country_id? This is where the INNER JOIN comes in handy, it will remove all those NULL records from the query result.
We use the exact same query and only change LEFT JOIN to INNER JOIN, which will look like:
Now our result is:
Since all the SQL tutorials here seem to be very much outdated. I wanted to write a tutorial on how to write basic SQL statements. SQL is a VERY complex and nasty language, there's so much more to know than I can cover in a simple tutorial, but I will try my best.
I will start with a tutorial about selecting data, but if you guys want me to make more for updating, deleting, creating, subqueries, counting, summing or whatever, tell me.
Let's say we own a website which has users and these users can all update their profile with a certain country. Our tables look like this:
user | ||
id | country_id | name |
1 | 1 | Example Name1 |
2 | 1 | Example Name2 |
3 | 2 | Example Name3 |
4 | 2 | Example Name4 |
5 | NULL | Example Name5 |
country | |
id | name |
1 | Australia |
2 | Germany |
3 | China |
The most basic select statement looks like this:
PHP:
SELECT columnname, columnname, columnname FROM tablename;
Letās say we want to select the name and the id of all users, we would write it like this:
PHP:
SELECT id, name FROM user;
This will return the name and id of ALL the records. It will look like this:
id | name |
1 | Example Name1 |
2 | Example Name2 |
3 | Example Name3 |
4 | Example Name4 |
5 | Example Name5 |
But what if we only want to know the name and id from the users who live in Australia? We first need to look in our country table and we see that Australia has id 1. Now we have to add a āWHEREā statement to our SELECT. The most basic SELECT WHERE statement looks like this:
PHP:
SELECT columnname, columname, columnname FROM tablename WHERE columname = 'value' OR columname = 'value' AND columnname = 'value';
As you can see, you can add ORs and ANDs to your statement, this works just like you would expect it to work. If you are a programmer, you can see OR as || and AND as &&.
Now letās select our Australian users:
PHP:
SELECT id, name FROM user WHERE country_id = 1;
This will return only Australian users. It will look like this:
id | name |
1 | Example Name1 |
2 | Example Name2 |
But what if we want to select id, name and country name? How do we do that? We can do that with a JOIN. There are a lot of different joins; INNER JOIN, OUTER JOIN, RIGHT JOIN, LEFT JOIN, RIGHT OUTER JOIN, etc.
This is a lot of information to take in at once, so weāll keep it simple and narrow this guide down to two JOINs: the LEFT JOIN and the INNER JOIN.
So what is the difference? Iāll show you this in the following examples, since writing it down is difficult.
The most basic JOIN looks like this:
PHP:
SELECT tablename1.columnname , tablename1.columname, tablename2.columname FROM tablename1
JOIN tablename2 ON tablename2.keycolumn1 = tablename1.keycolumn2;
The key columns represent a column belonging to table2 which is also present in table1.
Now to ALSO select the country name for all users, we write our SELECT statement like this:
PHP:
SELECT user.id, user.name, country.name FROM user
LEFT JOIN country ON country.id = user.country_id;
We can add some aliases to make our query a bit more readable:
PHP:
SELECT u.id, u.name, c.name FROM user u
LEFT JOIN country c ON c.id = u.country_id;
Using aliases is NOT a must, it just comes down to a matter of preference whether you want to use it or not.
However if you are selecting two columns with the same column name, in our example this is u.name and c.name, you have to add an alias to one of them. I would do it like this:
PHP:
SELECT u.id, u.name, c.name country_name FROM user u
LEFT JOIN country c ON c.id = u.country_id;
This query will return the following:
id | name | country_name |
1 | Example Name1 | Australia |
2 | Example Name2 | Australia |
3 | Example Name3 | Germany |
4 | Example Name4 | Germany |
5 | Example Name5 | NULL |
But wait.. Example Name5 hasnāt got a country_id filled in, what if we only want users who do have a country_id? This is where the INNER JOIN comes in handy, it will remove all those NULL records from the query result.
We use the exact same query and only change LEFT JOIN to INNER JOIN, which will look like:
PHP:
SELECT u.id, u.name, c.name country_name FROM user u
INNER JOIN country c ON c.id = u.country_id;
Now our result is:
id | name | country_name |
1 | Example Name1 | Australia |
2 | Example Name2 | Australia |
3 | Example Name3 | Germany |
4 | Example Name4 | Germany |