0% found this document useful (0 votes)
8 views

SQL Keywords and Functions

Uploaded by

khwezi sokhela
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

SQL Keywords and Functions

Uploaded by

khwezi sokhela
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Here's a list of **key SQL keywords** and **common SQL functions** that you'll frequently use

when working with SQL databases.

### **Key SQL Keywords**

1. **`SELECT`** – Retrieves data from a database.

```sql

SELECT * FROM table_name;

```

2. **`FROM`** – Specifies the table from which to retrieve or modify data.

```sql

SELECT column1 FROM table_name;

```

3. **`WHERE`** – Filters records based on a condition.

```sql

SELECT * FROM table_name WHERE condition;

```

4. **`INSERT INTO`** – Adds new records to a table.

```sql

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

```

5. **`UPDATE`** – Modifies existing records in a table.

```sql

UPDATE table_name SET column1 = value1 WHERE condition;

```

6. **`DELETE`** – Deletes records from a table.


```sql

DELETE FROM table_name WHERE condition;

```

7. **`CREATE TABLE`** – Creates a new table.

```sql

CREATE TABLE table_name (column1 datatype, column2 datatype);

```

8. **`ALTER TABLE`** – Modifies an existing table (e.g., adding or dropping columns).

```sql

ALTER TABLE table_name ADD column_name datatype;

```

9. **`DROP TABLE`** – Deletes an entire table from the database.

```sql

DROP TABLE table_name;

```

10. **`JOIN`** – Combines rows from two or more tables based on a related column.

- **`INNER JOIN`** – Returns rows with matching values in both tables.

```sql

SELECT * FROM table1 INNER JOIN table2 ON table1.column = table2.column;

```

- **`LEFT JOIN`** – Returns all rows from the left table and matched rows from the right table.

```sql

SELECT * FROM table1 LEFT JOIN table2 ON table1.column = table2.column;

```

11. **`GROUP BY`** – Groups rows that have the same values into summary rows.

```sql
SELECT column1, COUNT(*) FROM table_name GROUP BY column1;

```

12. **`ORDER BY`** – Sorts the result set in ascending or descending order.

```sql

SELECT * FROM table_name ORDER BY column1 ASC;

```

13. **`HAVING`** – Filters groups based on a condition (used with `GROUP BY`).

```sql

SELECT column1, COUNT(*) FROM table_name GROUP BY column1 HAVING COUNT(*) > 1;

```

14. **`DISTINCT`** – Selects only unique values.

```sql

SELECT DISTINCT column1 FROM table_name;

```

15. **`LIMIT`** (or **`TOP`** in SQL Server) – Limits the number of rows returned.

```sql

SELECT * FROM table_name LIMIT 10;

```

16. **`UNION`** – Combines the results of two queries.

```sql

SELECT column1 FROM table1 UNION SELECT column1 FROM table2;

```

17. **`EXISTS`** – Checks whether a subquery returns any records.

```sql

SELECT * FROM table_name WHERE EXISTS (subquery);


```

18. **`IN`** – Checks if a value matches any value in a list or subquery.

```sql

SELECT * FROM table_name WHERE column IN (value1, value2, value3);

```

19. **`BETWEEN`** – Filters results within a certain range.

```sql

SELECT * FROM table_name WHERE column BETWEEN value1 AND value2;

```

20. **`LIKE`** – Searches for a specified pattern in a column.

```sql

SELECT * FROM table_name WHERE column LIKE 'pattern%';

```

21. **`NOT`** – Negates a condition.

```sql

SELECT * FROM table_name WHERE NOT condition;

```

22. **`CASE`** – Allows conditional logic in queries.

```sql

SELECT column,

CASE

WHEN condition1 THEN 'result1'

WHEN condition2 THEN 'result2'

ELSE 'result3'

END AS alias_name

FROM table_name;
```

---

### **Common SQL Functions**

#### 1. **Aggregate Functions** (Used with `GROUP BY`)

- **`COUNT()`** – Returns the number of rows.

```sql

SELECT COUNT(*) FROM table_name;

```

- **`SUM()`** – Returns the total sum of a numeric column.

```sql

SELECT SUM(column_name) FROM table_name;

```

- **`AVG()`** – Returns the average value of a numeric column.

```sql

SELECT AVG(column_name) FROM table_name;

```

- **`MIN()`** – Returns the smallest value.

```sql

SELECT MIN(column_name) FROM table_name;

```

- **`MAX()`** – Returns the largest value.

```sql

SELECT MAX(column_name) FROM table_name;


```

#### 2. **String Functions**

- **`CONCAT()`** – Concatenates two or more strings.

```sql

SELECT CONCAT(first_name, ' ', last_name) AS full_name FROM table_name;

```

- **`SUBSTRING()`** – Extracts a substring from a string.

```sql

SELECT SUBSTRING(column_name, start, length) FROM table_name;

```

- **`LENGTH()`** – Returns the length of a string.

```sql

SELECT LENGTH(column_name) FROM table_name;

```

- **`UPPER()`** – Converts a string to uppercase.

```sql

SELECT UPPER(column_name) FROM table_name;

```

- **`LOWER()`** – Converts a string to lowercase.

```sql

SELECT LOWER(column_name) FROM table_name;

```

- **`TRIM()`** – Removes leading and trailing spaces.

```sql
SELECT TRIM(column_name) FROM table_name;

```

#### 3. **Numeric Functions**

- **`ROUND()`** – Rounds a number to a specified number of decimal places.

```sql

SELECT ROUND(column_name, 2) FROM table_name;

```

- **`ABS()`** – Returns the absolute value.

```sql

SELECT ABS(column_name) FROM table_name;

```

- **`MOD()`** – Returns the remainder of a division.

```sql

SELECT MOD(column_name, divisor) FROM table_name;

```

#### 4. **Date Functions**

- **`NOW()`** – Returns the current date and time.

```sql

SELECT NOW() FROM table_name;

```

- **`CURDATE()`** – Returns the current date.

```sql

SELECT CURDATE() FROM table_name;

```
- **`DATEDIFF()`** – Returns the difference in days between two dates.

```sql

SELECT DATEDIFF(date1, date2) FROM table_name;

```

- **`DATE_ADD()`** – Adds a specified time interval to a date.

```sql

SELECT DATE_ADD(date_column, INTERVAL 7 DAY) FROM table_name;

```

- **`EXTRACT()`** – Extracts a part of a date (e.g., year, month, day).

```sql

SELECT EXTRACT(YEAR FROM date_column) FROM table_name;

```

---

### **Summary Table of Common Functions**

| Function | Description |

|--------------|--------------------------------------------------|

| `COUNT()` | Counts rows or non-null values |

| `SUM()` | Returns the sum of a column |

| `AVG()` | Returns the average of a column |

| `MIN()` | Finds the smallest value |

| `MAX()` | Finds the largest value |

| `CONCAT()` | Concatenates strings |

| `SUBSTRING()`| Extracts a part of a string |

| `UPPER()` | Converts a string to uppercase |

| `LOWER()` | Converts a string to lowercase |


| `ROUND()` | Rounds a number |

| `NOW()` | Returns the current date and time |

| `CURDATE()` | Returns the current date |

| `DATEDIFF()` | Calculates the difference between two dates |

| `EXTRACT()` | Extracts a part of a date (e.g., year, month) |

These keywords and functions will help you handle a wide variety of SQL queries and operations
efficiently. Let me know if you need more specific examples!

You might also like