Junior — Middle
Do you have experience using table join operators in SQL?
sobes.tech AI
Answer from AI
Yes, I have experience. In SQL, join operators allow combining data from multiple tables based on a common field. Main types of JOIN:
- INNER JOIN — returns only matching records from both tables.
- LEFT JOIN — returns all records from the left table and matching ones from the right, if any.
- RIGHT JOIN — returns all from the right and matching from the left.
- FULL JOIN — returns all records from both tables, with NULLs where there are no matches.
Example in Node.js using pg library for PostgreSQL:
const { Client } = require('pg');
const client = new Client({ /* connection params */ });
async function getUsersWithOrders() {
await client.connect();
const res = await client.query(`
SELECT users.id, users.name, orders.id AS order_id
FROM users
LEFT JOIN orders ON users.id = orders.user_id
`);
await client.end();
return res.rows;
}
This query retrieves all users and their orders if any.