0% found this document useful (0 votes)
17 views7 pages

SQL Presentation

Subqueries allow queries to be nested within other SQL queries. There are several types of subqueries including scalar, row, table, correlated, and nested subqueries. Subqueries provide a way to break down complex problems but can impact performance on large result sets. Alternative approaches like joins should be considered.

Uploaded by

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

SQL Presentation

Subqueries allow queries to be nested within other SQL queries. There are several types of subqueries including scalar, row, table, correlated, and nested subqueries. Subqueries provide a way to break down complex problems but can impact performance on large result sets. Alternative approaches like joins should be considered.

Uploaded by

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

A subquery, also known as a nested query or inner query, is a query nested inside another SQL query.

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.

Here are some common types of subqueries:

1. **Scalar Subquery:**

- Returns a single value.

- 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

SELECT column1, (SELECT MAX(column2) FROM table2) AS max_value

FROM table1;

```

2. **Row Subquery:**

- Returns one or more rows of data.

- 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

WHERE column2 IN (SELECT column2 FROM table2 WHERE column3 = 'value');

```

3. **Table Subquery:**

- Returns a table.

- Can be used in the FROM clause.

```sql
SELECT *

FROM (SELECT column1, column2 FROM table1) AS subquery

WHERE subquery.column1 = 'value';

```

4. **Correlated Subquery:**

- Refers to columns from the outer query.

- The subquery depends on the outer query.

```sql

SELECT column1

FROM table1 t1

WHERE column2 = (SELECT MAX(column2) FROM table1 t2 WHERE t2.column3 = t1.column3);

```

5. **Nested Subqueries:**

- Subqueries can be nested within each other.

```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

CREATE VIEW view_name AS

SELECT column1, column2, ...

FROM table_name

WHERE condition;

```

Once a view is created, you can query it like a regular table:

```sql

SELECT * FROM view_name;

```

Here are some key points about SQL views:

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.

6. **Updatability (in some cases):**

- 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.

Here's an example of creating a simple view:

```sql

CREATE VIEW employee_view AS

SELECT employee_id, first_name, last_name

FROM employees

WHERE department_id = 10;

```

Now, you can query the view:

```sql

SELECT * FROM employee_view;

```

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

CREATE TABLE employees (

employee_id INT PRIMARY KEY,

first_name VARCHAR(50),

last_name VARCHAR(50),

hire_date DATE

);

```

2. **ALTER:**

- Used to modify the structure of existing database objects.

```sql

ALTER TABLE employees

ADD COLUMN department_id INT;

```

3. **DROP:**

- Used to delete existing database objects like tables or indexes.

```sql

DROP TABLE employees;


```

### Data Control Language (DCL):

DCL statements are used for controlling access to data within the database. They involve privileges and
permissions. Two main DCL statements are:

1. **GRANT:**

- Used to provide specific privileges to database users.

```sql

GRANT SELECT, INSERT ON employees TO user1;

```

2. **REVOKE:**

- Used to remove specific privileges from database users.

```sql

REVOKE INSERT ON employees FROM user1;

```

In summary:

- **DDL (Data Definition Language):**

- Concerned with the structure and definition of the database.

- Includes statements like CREATE, ALTER, and DROP.

- Examples: Creating tables, altering columns, and dropping indexes.

- **DCL (Data Control Language):**

- Concerned with permissions and access control to the data.


- Includes statements like GRANT and REVOKE.

- 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).

You might also like