L.07 Flashcards

Querying in SQL

1
Q

What SQL command is used to create a table?

A

CREATE TABLE

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

What constraint ensures a column has unique values?

A

UNIQUE

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

How do you define a primary key in SQL?

A

PRIMARY KEY(column_name)

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

What does FOREIGN KEY do?

A

Ensures referential integrity by linking to another table’s primary key.

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

What happens if a referenced primary key is deleted in a foreign key relationship?

A

Actions can be CASCADE, SET NULL, SET DEFAULT, or NO ACTION.

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

What is the purpose of the INSERT statement?

A

To add new rows into a table.

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

How do you retrieve all columns from a table?

A

SELECT * FROM table_name;

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

How do you rename an attribute in SQL?

A

Using AS, e.g., SELECT column_name AS alias FROM table_name;

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

What SQL clause is used to filter query results?

A

WHERE

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

How do you select only unique values from a table?

A

Use SELECT DISTINCT column_name FROM table_name;

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

What arithmetic operations can be used in SQL queries?

A

+, -, *, /, %

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

How do you filter results based on multiple conditions?

A

Use logical operators AND, OR, NOT

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

What SQL operator checks if a value is within a range?

A

BETWEEN

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

How do you check for NULL values?

A

IS NULL or IS NOT NULL

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

How do you check if a value is in a specific set of values?

A

IN (value1, value2, …)

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

What clause is used to sort query results?

17
Q

How do you perform a basic SQL join?

A

SELECT columns FROM table1 INNER JOIN table2 ON table1.key = table2.key;

18
Q

What are the different types of SQL joins?

A

INNER JOIN, LEFT JOIN, RIGHT JOIN, FULL OUTER JOIN

19
Q

What is a self-join?

A

A join where a table is joined with itself using table aliases.

20
Q

What is a natural join?

A

A join that automatically matches columns with the same name.

21
Q

What set operators does SQL support?

A

UNION, INTERSECT, EXCEPT

22
Q

What are subqueries used for?

A

To execute a nested query within another query.

23
Q

What are aggregate functions?

A

COUNT, SUM, AVG, MAX, MIN

24
Q

How do you group data in SQL?

A

Using GROUP BY

25
How do you filter grouped results?
Using HAVING
26
What is the difference between WHERE and HAVING?
WHERE filters before grouping; HAVING filters after.