When we have 2 or more tables, we need to use JOINs to connect them in a query. This function searches for data in tables by a common field (key).
Example of using JOIN:
SELECT column, another_table_column,
FROM mytable
INNER JOIN another_table
ON mytable.id = another_table.id
WHERE condition(s)
ORDER BY column ASC/DESC
LIMIT num_limit OFFSET num_offset;
Try not to make your life difficult and not to write too complex queries when you can write 2/3 simple ones and look with your eyes to validate the final result. Don't throw the user out of the process (otherwise we will be replaced by robots)!
SELECT column, another_column,
FROM mytable
INNER/LEFT/RIGHT/FULL JOIN another_table
ON mytable.id = another_table.matching_id
WHERE condition(s)
ORDER BY column ASC/DESC
LIMIT num_limit OFFSET num_offset;
In addition to INNER JOIN
, there are also LEFT
/RIGHT
/FULL
. They help not to concatenate all the data from different tables into one resulting table, but to set your own conditions when collecting common data.
In general, this was all the SQL I can give in my blog. The rest is a matter of practice!
In addition to mathematical and statistical problems, SQL solves the following problems:
- Create/delete an entire data table with the definition of the structure and data types inside
- Adding rows/columns and deleting them similarly. This can be done in the editor, but the SQL code allows you to do this more flexibly and carefully to the database
- Sorting and filtering all table data
Be careful that your queries don't edit the entire table if you are not the only one working with it. Cross queries are not the best case, so follow the versioning order.
Top comments (0)