Thursday, February 29, 2024

x̄ - > Examples of SQL queries

Here are key examples of SQL queries for retrieving data from a specific table, updating records, joining multiple tables, and performing other standard database operations:

1. Retrieve data from a specific table

SELECT * FROM tableName;

2. Retrieve specific columns from a table

SELECT column1, column2 FROM tableName;

3. Retrieve data with conditions (using WHERE clause)

SELECT * FROM tableName WHERE condition;

4. Update records in a table

UPDATE tableName SET column1 = value1, column2 = value2 WHERE condition;

5. Delete records from a table

DELETE FROM tableName WHERE condition;

6. Join multiple tables (INNER JOIN)

SELECT t1.column1, t2.column2 
FROM table1 t1 
INNER JOIN table2 t2 ON t1.commonColumn = t2.commonColumn;

7. Left Join

SELECT t1.column1, t2.column2 
FROM table1 t1 
LEFT JOIN table2 t2 ON t1.commonColumn = t2.commonColumn;

8. Right Join

SELECT t1.column1, t2.column2 
FROM table1 t1 
RIGHT JOIN table2 t2 ON t1.commonColumn = t2.commonColumn;

9. Full Outer Join

SELECT t1.column1, t2.column2 
FROM table1 t1 
FULL OUTER JOIN table2 t2 ON t1.commonColumn = t2.commonColumn;

10. Insert records into a table

INSERT INTO tableName (column1, column2) VALUES (value1, value2);

11. Aggregate functions (e.g., SUM, AVG, MAX, MIN)

SELECT SUM(column1) FROM tableName;

12. Group by clause

SELECT column1, COUNT(*)
FROM tableName
GROUP BY column1;

These are basic examples, and SQL syntax may vary slightly depending on your specific database system (such as MySQL, PostgreSQL, or SQL Server). Remember to replace tableName, column1, column2, value1, value2, and condition with your actual database schema values.

No comments:

Meet the Authors
Zacharia Nyambu’s blog features multiple contributors with clear activity status.
Active ✔
πŸ§‘‍πŸ’»
Zacharia Nyambu
Lead Author
Inactive ✖
πŸ‘©‍πŸ’»
Linda Bahati
Co‑Author
Inactive ✖
πŸ‘¨‍πŸ’»
Jefferson Mwangolo
Co‑Author
Inactive ✖
πŸ‘©‍πŸŽ“
Florence Wavinya
Guest Author
Inactive ✖
πŸ‘©‍πŸŽ“
Esther Njeri
Guest Author
Inactive ✖
πŸ‘©‍πŸŽ“
Clemence Mwangolo
Guest Author

Followers