SQL - Joins & Indexes

0.0(0)
studied byStudied by 0 people
full-widthCall with Kai
GameKnowt Play
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
Card Sorting

1/13

encourage image

There's no tags or description

Looks like no tags are added yet.

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

14 Terms

1
New cards

What are Joins in SQL?

A join clause is used to combine rows from two or more tables, based on a related column between them.

2
New cards

What are the types of Joins in SQL Server?

  • Left outer join

  • Right outer join

  • Full outer join

  • Inner Join

3
New cards

Left join

A left join returns all the rows from the left table, along with any matching rows from the right table.

  • All left with matching right

-- LEFT JOIN: All rows from Table A, matched rows from Table B
SELECT [list]
FROM [Table A] A
LEFT JOIN [Table B] B
ON A.Value = B.Value;

4
New cards

Right join

A right join returns all the rows from the right table, along with any matching rows from the left table.

  • All right with matching left

-- RIGHT JOIN: All rows from Table B, matched rows from Table A
SELECT [list]
FROM [Table A] A
RIGHT JOIN [Table B] B
ON A.Value = B.Value;

5
New cards

Full join

A full outer join returns all the rows from both the left and right tables in the join.

  • Both

-- FULL OUTER JOIN: All rows from both tables, matched where possible
SELECT [list]
FROM [Table A] A
FULL OUTER JOIN [Table B] B
ON A.Value = B.Value;

6
New cards

Inner join

An inner join returns only the common rows from both tables that meet the join condition.

  • Same

-- INNER JOIN: Only rows with matching values in both tables
SELECT [list]
FROM [Table A] A
INNER JOIN [Table B] B
ON A.Value = B.Value;

7
New cards

What is Self-Join and when to use?

  • A self join is a join of a table to itself.

<ul><li><p>A self join is a join of a table to itself.</p></li></ul><p></p>
8
New cards

What are Indexes in SQL Server?

  • Indexes in SQL Server are data structures that improve the speed of data retrieval operations on tables and views.

  • They are similar to indexes at the start of the BOOKS, which purpose is to find a topic quickly.

9
New cards

What is Clustered index?

  • A clustered index is a type of index that determines the physical order of data in a table.

    • physical order

  • Table data can be sorted in only way, therefore, there can be only one clustered index per table.

    • only one

  • In SQL Server, if you set a primary key on a column, then it will automatically create a clustered index on that particular column.

    • primary key automatically create

10
New cards

What is Non-Clustered index?

  • A non-clustered index is stored at one place and table data is stored in another place.

  • A table can have multiple non-clustered index in a table.

EX: Card Catalog (Non-Clustered Index): A separate system that lists books by author name and points to their location on the shelf.

11
New cards

What is the difference between Clustered and Non-Clustered index?

Feature

Clustered Index

Non-Clustered Index

Data Storage

Determines physical order of data in the table

Stored separately from table data; points to row locations

Quantity per Table

Only one clustered index allowed

Multiple non-clustered indexes allowed

Performance

Generally faster for range queries and sorting

Slightly slower due to indirect access

Real-Life Analogy

Books arranged by ISBN on shelves

Card catalog organized by author pointing to book location

12
New cards

How to create Clustered and Non-Clustered index in a table?

-- Clustered Index
CREATE CLUSTERED INDEX index_name
ON table_name(column_name ASC/DESC);

-- Non-clustered Index
CREATE NONCLUSTERED INDEX index_name
ON table_name(column_name ASC/DESC);
  • When you create a PRIMARY KEY constraint, a clustered index on the column is automatically created.

13
New cards

In which column you will apply the indexing to optimize this query. “select id, class from student where name=”happy””?

The column after WHERE condition, which is “NAME” here.

14
New cards