SQL Presentation
SQL Presentation
Subqueries
are enclosed in parentheses and can be used in various parts of a SQL statement, such as SELECT, FROM, WHERE,
and HAVING clauses. Subqueries are useful for performing operations based on the result of another query.
1. **Scalar Subquery:**
- Typically used in a place where an expression is expected to produce a single value, such as the SELECT list or
the WHERE clause.
```sql
FROM table1;
```
2. **Row Subquery:**
- Can be used in a place where a set of values is expected, such as the IN or EXISTS clause.
```sql
SELECT column1
FROM table1
```
3. **Table Subquery:**
- Returns a table.
```sql
SELECT *
```
4. **Correlated Subquery:**
```sql
SELECT column1
FROM table1 t1
```
5. **Nested Subqueries:**
```sql
SELECT column1
FROM table1
WHERE column2 IN (SELECT column2 FROM table2 WHERE column3 IN (SELECT column3 FROM table3));
```
Subqueries provide a way to break down complex problems into simpler, more manageable parts. However, it's
important to use subqueries judiciously, as they can impact performance, especially if the result set is large. In
some cases, equivalent results can be achieved using JOIN operations, so it's a good idea to consider alternative
approaches based on the specific requirements of your query.
A SQL view is a virtual table that is based on the result of a SELECT query. It does not store the data itself but
provides a way to represent the result of a query as if it were a table. Views can be used to simplify complex
queries, encapsulate logic, and provide a layer of abstraction over the underlying tables.
Here's a basic syntax for creating a view:
```sql
FROM table_name
WHERE condition;
```
```sql
```
1. **Virtual Table:**
- A view is a virtual table that does not store the data itself; instead, it represents the data stored in the
underlying tables.
2. **Data Independence:**
- Views provide a level of abstraction, allowing users to access data without directly interacting with the
underlying tables. This can help insulate applications from changes to the database schema.
3. **Simplified Queries:**
- Views can simplify complex queries by encapsulating logic. For example, you can use a view to join multiple
tables and then query the view instead of writing complex join operations in each query.
4. **Security:**
- Views can be used to restrict access to certain columns or rows of a table. Users can be granted permission to
access a view without directly granting them access to the underlying tables.
5. **Reusability:**
- Once a view is created, it can be used in multiple queries. This promotes code reusability and reduces the need
to write the same complex logic in different parts of an application.
- Views can be updatable, meaning that you can use them to update the underlying tables if certain conditions
are met. However, not all views are updatable, and there are restrictions on which views can be used for updates.
```sql
FROM employees
```
```sql
```
Keep in mind that the underlying data in the tables is not modified by creating or querying views. Views are just a
way to present the data in a particular form without altering the actual data stored in the tables.
In SQL, Data Definition Language (DDL) and Data Control Language (DCL) are two categories of SQL statements that
are used for defining and managing the database structure and controlling access to the data.
### Data Definition Language (DDL):
DDL statements are used for defining and managing the structure of the database. They are responsible for
creating, altering, and deleting database objects like tables, indexes, and views. Common DDL statements include:
1. **CREATE:**
- Used to create new database objects such as tables, indexes, and views.
```sql
first_name VARCHAR(50),
last_name VARCHAR(50),
hire_date DATE
);
```
2. **ALTER:**
```sql
```
3. **DROP:**
```sql
DCL statements are used for controlling access to data within the database. They involve privileges and
permissions. Two main DCL statements are:
1. **GRANT:**
```sql
```
2. **REVOKE:**
```sql
```
In summary:
- Examples: Granting SELECT privileges to a user, revoking INSERT privileges from a user.
It's worth noting that some databases may have additional DDL and DCL statements or variations, but the core
concepts remain similar across most relational database management systems (RDBMS).