Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
4 views

SQL

Uploaded by

afoeze1
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

SQL

Uploaded by

afoeze1
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 19

SQL (Structured Query Language) is a standard language used to interact with relational databases.

Below is a comprehensive list of SQL syntax categories and their common commands. Note that this is
not an exhaustive list, but it covers the most widely used SQL syntax.

### **1. Data Definition Language (DDL)**

DDL commands are used to define and manage database schema.

- **CREATE**: Create a new database object (e.g., table, view, index).

```sql

CREATE DATABASE database_name;

CREATE TABLE table_name (

column1 datatype,

column2 datatype ... );

- **ALTER**: Modify an existing database object.

```sql

ALTER TABLE table_name ADD column_name datatype;

ALTER TABLE table_name DROP COLUMN column_name;

```

- **DROP**: Delete a database object.

```sql
DROP DATABASE database_name;

DROP TABLE table_name;

```

- **TRUNCATE**: Remove all rows from a table without logging individual row deletions.

```sql

TRUNCATE TABLE table_name;

```

---

### **2. Data Manipulation Language (DML)**

DML commands are used to manipulate data in tables.


- **SELECT**: Retrieve data from a table.

```sql

SELECT column1, column2 FROM table_name WHERE condition;

SELECT * FROM table_name;

```

- **INSERT**: Add new rows to a table.

```sql

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

```

- **UPDATE**: Modify existing rows in a table.

```sql

UPDATE table_name SET column1 = value1 WHERE condition;

```
- **DELETE**: Remove rows from a table.

```sql

DELETE FROM table_name WHERE condition;

```

---

### **3. Data Query Language (DQL)**

DQL is primarily used for querying data. The main command is:

- **SELECT**: Retrieve data from one or more tables.

```sql

SELECT column1, column2 FROM table_name WHERE condition;


```

---

### **4. Data Control Language (DCL)**

DCL commands are used to control access to the database.

- **GRANT**: Give privileges to users.

```sql

GRANT SELECT, INSERT ON table_name TO user_name;

```

- **REVOKE**: Remove privileges from users.

```sql

REVOKE SELECT, INSERT ON table_name FROM user_name;


```

---

### **5. Transaction Control Language (TCL)**

TCL commands manage transactions in a database.

- **COMMIT**: Save changes made during the transaction.

```sql

COMMIT;

```

- **ROLLBACK**: Undo changes made during the transaction.

```sql
ROLLBACK;

```

- **SAVEPOINT**: Set a point within a transaction to which you can roll back.

```sql

SAVEPOINT savepoint_name;

```

- **SET TRANSACTION**: Define properties for a transaction.

```sql

SET TRANSACTION READ ONLY;

```

---
### **6. Constraints**

Constraints are rules enforced on data in tables.

- **NOT NULL**: Ensures a column cannot have a NULL value.

- **UNIQUE**: Ensures all values in a column are unique.

- **PRIMARY KEY**: A combination of NOT NULL and UNIQUE. Uniquely identifies each row.

- **FOREIGN KEY**: Ensures referential integrity between tables.

- **CHECK**: Ensures a condition is met.

- **DEFAULT**: Sets a default value for a column.

Example:

```sql

CREATE TABLE table_name (

column1 datatype NOT NULL,


column2 datatype UNIQUE,

column3 datatype DEFAULT 'default_value',

column4 datatype CHECK (column4 > 0),

PRIMARY KEY (column1),

FOREIGN KEY (column2) REFERENCES other_table(column_name)

);

```

---

### **7. Joins**

Joins are used to combine rows from two or more tables.

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

```sql
SELECT columns FROM table1

INNER JOIN table2 ON table1.column = table2.column;

```

- **LEFT JOIN (OUTER JOIN)**: Returns all rows from the left table and matching rows from the right
table.

```sql

SELECT columns FROM table1

LEFT JOIN table2 ON table1.column = table2.column;

```

- **RIGHT JOIN (OUTER JOIN)**: Returns all rows from the right table and matching rows from the left
table.

```sql

SELECT columns FROM table1


RIGHT JOIN table2 ON table1.column = table2.column;

```

- **FULL JOIN (OUTER JOIN)**: Returns rows when there is a match in either table.

```sql

SELECT columns FROM table1

FULL JOIN table2 ON table1.column = table2.column;

```

- **CROSS JOIN**: Returns the Cartesian product of two tables.

```sql

SELECT columns FROM table1

CROSS JOIN table2;

```
---

### **8. Subqueries**

Subqueries are queries nested inside another query.

- **Example**:

```sql

SELECT column1 FROM table_name

WHERE column2 = (SELECT column2 FROM other_table WHERE condition);

```

---

### **9. Aggregate Functions**


Aggregate functions perform calculations on a set of values.

- **COUNT()**: Count the number of rows.

```sql

SELECT COUNT(*) FROM table_name;

```

- **SUM()**: Calculate the sum of a column.

```sql

SELECT SUM(column_name) FROM table_name;

```

- **AVG()**: Calculate the average of a column.

```sql

SELECT AVG(column_name) FROM table_name;


```

- **MIN()**: Find the minimum value.

```sql

SELECT MIN(column_name) FROM table_name;

```

- **MAX()**: Find the maximum value.

```sql

SELECT MAX(column_name) FROM table_name;

```

---
### **10. String Functions**

String functions manipulate text data.

- **UPPER()**: Convert to uppercase.

```sql

SELECT UPPER(column_name) FROM table_name;

```

- **LOWER()**: Convert to lowercase.

```sql

SELECT LOWER(column_name) FROM table_name;

```

- **CONCAT()**: Concatenate strings.

```sql
SELECT CONCAT(column1, column2) FROM table_name;

```

- **SUBSTRING()**: Extract a substring.

```sql

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

```

---

### **11. Date and Time Functions**

Date and time functions manipulate date/time data.

- **NOW()**: Get the current date and time.


```sql

SELECT NOW();

```

- **CURDATE()**: Get the current date.

```sql

SELECT CURDATE();

```

- **DATEDIFF()**: Calculate the difference between two dates.

```sql

SELECT DATEDIFF(date1, date2);

```

---
### **12. Other Clauses**

- **GROUP BY**: Group rows with the same values.

```sql

SELECT column_name, COUNT(*) FROM table_name GROUP BY column_name;

```

- **HAVING**: Filter groups after aggregation.

```sql

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

```

- **ORDER BY**: Sort the result set.

```sql
SELECT column_name FROM table_name ORDER BY column_name ASC;

```

- **LIMIT**: Limit the number of rows returned.

```sql

SELECT column_name FROM table_name LIMIT 10;

```

---

This is a high-level overview of SQL syntax. Depending on the database system (e.g., MySQL, PostgreSQL,
SQL Server, Oracle), there may be additional features or slight variations in syntax.

You might also like