Advanced SQL Queries

Filtering Data with WHERE

SELECT * FROM users WHERE email = 'john@example.com';

Filters records based on the given condition.

Using Logical Operators

SELECT * FROM users WHERE email = 'john@example.com' AND name = 'John Doe';

This fetches records matching both conditions.

Sorting Results

SELECT * FROM users ORDER BY created_at DESC;

Sorts records in descending order by creation date.

Updating Data

UPDATE users SET name = 'Jane Doe' WHERE id = 1;

Updates the name of a user with ID 1.

Deleting Data

DELETE FROM users WHERE id = 1;

Deletes the record where the ID is 1.

Using Joins

SELECT users.name, orders.amount FROM users
JOIN orders ON users.id = orders.user_id;

This retrieves user names along with their order amounts by joining users and orders tables.