SQL join table JOIN operator.
We have several options for joining tables in SQL.
The most popular kind of join is through the INNER JOIN operator.
SELECT column_names (1..N) FROM table_name_1 JOIN table_name_2 ON join_condition
Sample code.
Connect the users and comments tables through the id and id_user fields
SELECT * FROM users JOIN comments ON users.id=comments.id_user
Select only the columns: comment date, username and message text.
SELECT comments.date, users.name, comments.text FROM users JOIN comments ON users.id=comments.id_user
Let’s put a condition on the name of the user.
SELECT comments.date, users.name, comments.text FROM users JOIN comments ON users.id=comments.id_user WHERE users.name='Alex'
Let’s sort the request by the date of the comment in descending order.
SELECT comments.date, users.name, comments.text FROM users JOIN comments ON users.id=comments.id_user WHERE users.name='Alex' ORDER BY comments.date DESC
Categories: