click below
click below
Normal Size Small Size show me how
DATABASE - 2
Advanced SQL
| Question | Answer |
|---|---|
| is used to combine rows from two or more tables based on a related column between them (usually a primary key and a foreign key). | SQL JOIN |
| Why Do We Use JOIN? | In a relational database |
| is stored in multiple tables | Data |
| are connected using keys. | Tables |
| allows us to retrieve related data in a single query. | JOIN |
| Types of SQL JOIN | Types of SQL JOIN |
| Matching rows only | INNER JOIN |
| All left + matching right | LEFT JOIN |
| All right + matching left | RIGHT JOIN |
| All rows from both tables | FULL JOIN |
| All possible row combinations | CROSS JOIN |
| Basic Structure of JOIN: | SELECT column 1, column,2 FROM table1 JOIN table2 ON table1.column = table2.column; |
| Returns only the rows where there is a match in both tables based on a related column. | INNER JOIN |
| SELECT a. account_number, s.firstName, s.lastName, s.course, a.active FROM Accounts a INNER JOIN students s ON a.student_id = s.id; | INNER JOIN |
| Returns all rows from the left table Returns matching rows from the right table f there is no match → NULL values are shown | LEFT JOIN |
| Returns all rows from the right table • Returns matching rows from the left table • If there is no match → NULL values appear in left table columns | RIGHT JOIN |
| Returns all matching rows • Returns all unmatched rows from the left table • Returns all unmatched rows from the right table • Displays NULL when no match exists “FULL JOIN = LEFT JOIN + RIGHT JOIN combined | FULL JOIN (FULL OUTER JOIN) |
| is a SELECT statement that is written inside another SQL statement. It is also called _____ AND _____ | Subquery - inner query - Nested query |
| Types of Subqueries | Types of Subqueries |
| returns a single column and a single row, that is, a single value. | Scalar subquery |
| returns multiple columns, but only a single row | Row subquery |
| returns one or more columns and multiple rows. | Table subquery |
| is a database object that improves the speed of data retrieval operations on a table. | Index |
| • Instead of scanning the whole table. • The database quickly locates the data. | Index |
| The database checks every row (Full Table Scan) | Without index: |
| The database directly jumps to the correct location. | With index: |
| automatically have indexes | Primary Keys |
| INSERT, UPDATE, DELETE | Too many indexes can slow down |
| determines the physical order of data in a table. The table rows are stored in sorted order based on the indexed column. | Clustered Index |
| Only one clustered index per table • Physically sorts the table • Usually created on the primary key • Fast for range queries | Key Characteristics of Clustered Index |
| creates a separate structure that stores indexed column values and pointers to actual table rows. | Non-Clustered Index |
| is a virtual table based on the result of a SELECT query | View |
| It does not store data physically. • Instead, it stores the query definition. | View |
| When you query a View, the database executes the stored SELECT statement | View |