Examples of SQL queries for retrieving data from a specific table, updating records, joining multiple tables, and performing other operations:
1. Retrieve data from a specific table:
```sql
SELECT * FROM tableName;
```
2. Retrieve specific columns from a table:
```sql
SELECT column1, column2 FROM tableName;
```
3. Retrieve data with conditions (using WHERE clause):
```sql
SELECT * FROM tableName WHERE condition;
```
4. Update records in a table:
```sql
UPDATE tableName SET column1 = value1, column2 = value2 WHERE condition;
```
5. Delete records from a table:
```sql
DELETE FROM tableName WHERE condition;
```
6. Join multiple tables (INNER JOIN):
```sql
SELECT t1.column1, t2.column2
FROM table1 t1
INNER JOIN table2 t2 ON t1.commonColumn = t2.commonColumn;
```
7. Left Join:
```sql
SELECT t1.column1, t2.column2
FROM table1 t1
LEFT JOIN table2 t2 ON t1.commonColumn = t2.commonColumn;
```
8. Right Join:
```sql
SELECT t1.column1, t2.column2
FROM table1 t1
RIGHT JOIN table2 t2 ON t1.commonColumn = t2.commonColumn;
```
9. Full Outer Join:
```sql
SELECT t1.column1, t2.column2
FROM table1 t1
FULL OUTER JOIN table2 t2 ON t1.commonColumn = t2.commonColumn;
```
10. Insert records into a table:
```sql
INSERT INTO tableName (column1, column2) VALUES (value1, value2);
```
11. Aggregate functions (e.g., SUM, AVG, MAX, MIN):
```sql
SELECT SUM(column1) FROM tableName;
```
12. Group by clause:
```sql
SELECT column1, COUNT(*)
FROM tableName
GROUP BY column1;
```
These are just some basic examples, and SQL syntax may vary slightly depending on the specific database system you're using (e.g., MySQL, PostgreSQL, SQL Server). Make sure to replace `tableName`, `column1`, `column2`, `value1`, `value2`, and `condition` with the actual names and values relevant to your database schema and requirements.

No comments:
Post a Comment