SQL Statements Flashcards

(30 cards)

1
Q

What is a view in SQL?

A

A saved SELECT query you can query like a table.

Views provide a way to simplify complex queries and enhance security.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Do views store their own data?

A

No — they read live data from underlying tables.

Views are dynamic and reflect the current state of the data.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Why do views update automatically?

A

Because they always read from the base tables, not stored snapshots.

This ensures that views always present the most current data.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

One reason views improve security?

A

They can hide sensitive columns.

This allows users to access necessary data without exposing sensitive information.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

One reason views improve simplicity?

A

They hide complex joins behind a simple name.

This makes it easier for users to work with data without needing to understand the underlying complexity.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What’s the real-world analogy for a view?

A

A pre-organised tray of ingredients (not the whole messy store room).

This analogy illustrates how views present a simplified interface to complex data.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is a stored procedure?

A

A saved block of SQL code you can run by name.

Stored procedures encapsulate logic for reuse and can improve performance.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Why do stored procedures improve performance?

A

They are often pre-compiled.

This reduces the time needed to execute the SQL code.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How do stored procedures improve security?

A

Users can run the procedure without accessing underlying tables.

This limits direct access to sensitive data.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What can stored procedures contain?

A
  • Multiple statements
  • Transactions
  • Error handling
  • Parameters

This flexibility allows for complex operations to be performed in a single call.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Real-world analogy for stored procedures?

A

A saved recipe instead of rewriting full instructions every time.

This highlights the efficiency of using stored procedures.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What does SELECT do?

A

Chooses which columns to show.

This is fundamental for retrieving specific data from a database.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What does FROM do?

A

Chooses which table to read data from.

This specifies the source of the data in a query.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What does WHERE do?

A

Filters which rows you want.

This clause is essential for narrowing down results based on conditions.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

In SELECT, where do commas go?

A

Between column names — never inside numeric values.

Proper syntax is crucial for executing queries successfully.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Where should formatting (like £ symbols) normally happen?

A

In the application layer, not the database.

This separation of concerns helps maintain data integrity.

17
Q

What does AND require?

A

All conditions must be true.

This logical operator is used to combine multiple conditions in a query.

18
Q

What does OR require?

A

At least one condition must be true.

This operator allows for more flexible query conditions.

19
Q

Why use brackets with OR?

A

To make the logic behave as expected.

Brackets clarify the order of operations in complex queries.

20
Q

What does BETWEEN do?

A

Checks if a value is within a range (inclusive).

This operator simplifies range queries.

21
Q

What order must BETWEEN values be in?

A

Low value first, high value second.

This ensures the correct interpretation of the range.

22
Q

What does NOT do?

A

Flips a condition — shows everything except the match.

This operator is useful for excluding specific results.

23
Q

Why be careful using NOT with NULL?

A

NULL behaves unpredictably with NOT — use IS NULL / IS NOT NULL.

Understanding NULL handling is crucial for accurate query results.

24
Q

Safe practice when inserting rows?

A

Always list the columns you are inserting into.

This prevents errors if the table structure changes.

25
Why list **columns explicitly**?
Prevents errors when the table structure changes. ## Footnote This practice enhances the robustness of SQL statements.
26
Why is the **WHERE** clause critical in UPDATE?
Without WHERE, you update every row. ## Footnote This can lead to unintended data changes.
27
What should you do before running an **UPDATE**?
Test with a SELECT using the same WHERE condition. ## Footnote This helps ensure the correct rows are affected.
28
What does **DELETE** do?
Removes selected rows (table structure stays). ## Footnote This operation allows for targeted data removal.
29
What does **TRUNCATE** do?
Deletes all rows, resets identity counters, keeps the structure. ## Footnote This is a faster way to clear a table than DELETE.
30
What does **DROP** do?
Permanently removes the entire table or database (data + structure). ## Footnote This action is irreversible and should be used with caution.