SQL Key Features Overview

JubilantSodalite avatar
JubilantSodalite
·
·
Download

Start Quiz

Study Flashcards

12 Questions

What type of JOIN retrieves all records when there is a match between corresponding rows in both tables?

FULL OUTER JOIN

Which SQL keyword is used to create a view?

CREATE

What does a clustered index do in SQL Server?

Physically orders data according to each key column

What is the main purpose of using transactions in a database?

To ensure data consistency during multiple operations

Which type of index is more efficient for smaller tables?

Nonclustered Index

In a subquery, what does the AVG() function do?

Calculates the average value of a column

What does an INNER JOIN do in SQL?

Returns records where there's a match between rows from both tables being joined

When using a LEFT OUTER JOIN in SQL, what happens if there is no match between rows from both tables?

It prints NULL values in the right table

In SQL, what does a RIGHT OUTER JOIN return?

Returns all records from the second table and the matching ones from the first table

What is a key feature of SQL that allows users to define and manipulate data stored in relational databases?

Indexes

Which SQL operation retrieves records where there is no match between rows from two tables?

None of the above

What does a LEFT OUTER JOIN return in SQL?

All records from the first table and the matched ones from the second table

Study Notes

SQL

Structured Query Language (SQL) is a powerful programming language used by relational database management systems. It allows users to define and manipulate data stored in these databases. In this article, we will discuss some of its key features: joins, subqueries, indexes, views, and transactions.

Joins

Joins are operations performed on two tables based on their related columns. There are several types of joins available in SQL:

INNER JOIN

An inner join retrieves records where there's a match between rows from both tables being joined. For example, consider two tables, students and courses. An inner join would return all students who enrolled into courses.

SELECT * FROM students s
JOIN courses c ON s.student_id = c.course_id;

LEFT OUTER JOIN

A left outer join returns all records from the first table and the matched ones from the second table. If there is no match, it prints NULL values in the right table. For instance, if we want to get all students along with their course information, even those without a record in the courses table, we can do:

SELECT * FROM students s
LEFT OUTER JOIN courses c ON s.student_id = c.course_id;

RIGHT OUTER JOIN

This type of join is similar to a left outer join, except it returns all records from the second table and the matching ones from the first table.

SELECT * FROM students s
RIGHT OUTER JOIN courses c ON s.student_id = c.course_id;

FULL OUTER JOIN

Full outer joins retrieve all records when there is a match between corresponding rows in both tables.

SELECT * FROM students s
FULL OUTER JOIN courses c ON s.student_id = c.course_id;

Subqueries

Subqueries are queries nested within other queries. They allow users to perform complex tasks such as filtering results based on conditions defined in another query. Here's an example of a simple subquery:

SELECT student_name
FROM students
WHERE student_grade > (SELECT AVG(student_grade) FROM students);

In this case, we are selecting names of students whose grades are higher than the average grade across all students.

Indexes

Indexes are used to speed up the process of searching for specific data within large datasets. They work like an inverted index of the real index of a book. When you look through an index, instead of having to look through the entire book, you have all of the books' information right there with one quick glance. There are three types of indexes: clustered, nonclustered and full text indexes.

Clustered Indexes

In SQL Server, the table itself forms a type of index called a clustered index. All data is physically ordered according to each key column, making it easier to sort and retrieve specific data quickly.

CREATE TABLE customers
(...);

NonClustered Indexes

Nonclustered indexes store values from each selected column, along with a row identifier called a rowset ID that references the data in the base table. They can be used on many tables but are more efficient for smaller tables.

CREATE INDEX idx_customers_state
ON customers (state);

Views

Views allow users to create virtual tables by joining two or more tables based on certain criteria. These virtual tables can then be queried like any other table, offering a convenient way to combine data from multiple sources without duplicating it in another physical table. To create a view, use the CREATE VIEW statement:

CREATE VIEW vw_customers AS
SELECT * FROM customers
JOIN orders ON customers.customer_id = orders.customer_id;

Transactions

Transactions are a way to ensure data consistency when multiple actions are performed on a database. They are useful when performing multiple operations that need to succeed or fail as a single unit.

START TRANSACTION;

INSERT INTO customers (customer_id, name) VALUES (1, 'John');
INSERT INTO orders (order_id, customer_id, order_date, product) VALUES (1, 1, '2020-01-01', 'Apple MacBook');

COMMIT;

In this example, two statements are grouped together in a transaction. If either statement fails, the entire transaction fails, ensuring data consistency.

In conclusion, SQL provides a wide range of features to manage data efficiently. Whether it's joining tables, using subqueries, creating indexes, defining views, or managing transactions, SQL offers powerful tools for working with relational databases.

This article covers key features of SQL such as joins, subqueries, indexes, views, and transactions. Learn about different types of joins like inner, left outer, right outer, and full outer joins. Explore how subqueries, indexes, views, and transactions can be used to efficiently manage data in relational databases.

Make Your Own Quizzes and Flashcards

Convert your notes into interactive study material.

Get started for free

More Quizzes Like This

Use Quizgecko on...
Browser
Browser