SQL Flashcards

(50 cards)

1
Q

What does sql stand for

A

Structured Query Language

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

What is a relational database

A

A database that stores data in tables (rows + columns) with relationships via keys.

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

How do you create a database in SQL?

A

CREATE DATABASE dbname;

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

What does dropping a database do? DROP dbname

A

Permanently deletes the entire database and all tables.

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

What is a table in SQL

A

A structure of rows (records) and columns (fields)

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

How do you create a table in SQL

A

CREATE TABLE tableName ( columns… );

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

What does SELECT * do?

A

Selects all rows and all columns in a table

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

How do you select specific columns in SQL

A

SELECT col1, col2 FROM tableName;

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

What is the purpose of the where clause

A

Filters rows based on a condition.

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

Give an example of using the where clause employees have an hourly_pay greater than equal to 15

A

SELECT *
FROM employees
WHERE hourly_pay >= 15;

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

Name 3 SQL composition operators

A

=, != (or <>), <, >, >=, <=, IS NULL, IS NOT NULL.

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

How do you insert one row into a table in SQL

A

INSERT INTO table VALUES (…);

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

How do you insert multiple rows into a table in SQL

A

INSERT INTO table
VALUES (…),
(…),
(…);

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

How do you insert only specific columns?

A

INSERT INTO table (col1, col2) VALUES (…);

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

How do you update a single column in a table in SQL?

A

UPDATE table
SET col = value
WHERE condition;

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

Why must every UPDATE or DELETE include a WHERE clause?

A

Without it, all rows in the table will be modified or removed.

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

What is COMMIT used for?

A

Saves the current transaction permanently.

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

What is ROLLBACK used for?

A

Undoes all changes since the last COMMIT.

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

What do CURRENT_DATE(), CURRENT_TIME(), and NOW() return?

A

Today’s date, the current time, and current date + time.

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

What does the UNIQUE constraint ensure?

A

All values in the column must be different.

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

What does the NOT NULL constraint ensure?

A

The column cannot contain NULL values.

22
Q

show how to implement NOT NULL and UNIQUE into a new table

A

CREATE TABLE products(
product _id INT, product_name VARCHAR (25)UNIQUE AND NOTNULL,
price DECIMAL (4, 2)

23
Q

What does a check constraint too

A

Restricts what values are allowed in a column.

24
Q

Give an example of adding a names check constraint

A

*CREATE TABLE employees (
employee_id INT,
first_name VARCHAR (50),
last _name VARCHAR(50),
hourly_pay DECIMAL (5, 2),
hire_date DATE,

    CONSTRAINT chk hourly pay CHECK (hourly pay >= 10.00) );
25
How do I add a check constraint when the table is already made
ALTER TABLE employees ADD CONSTRAINT chk_hourly_pay CHECK (hourly_pay ›= 10.00);
26
What does default do
sets an automatic value when none is specified
27
Show how default can be used
CREATE TABLE products ( product_id INT, product _name VARCHAR (25), price DECIMAL(4,2) DEFAULT 0 ):
28
What are the two rules of a primary key
Must be unique and cannot be NULL
29
How many primary keys can a table have
one (although it may consist of multiple columns (composite))
30
What does AUTO_INCREMENT do
Automatically increases numeric primary key values each time a new row is inserted.
31
Give an example of how auto_increment is used?
CREATE TABLE transactions ( transaction_id INT PRIMARY KEY AUTO_INCREMENT, amount DECIMAL (5, 2) ");
32
How can you change the value auto-increment begins at
AUTO_INCREMENT = 1
33
What is a foreign key
A column that refers to a primary key in another table, creating a relationship.
34
What does a foreign key prevent
Deleting or modifying parent rows that would break the relationship.
35
What does an INNER JOIN return
Only rows where both tables have matching values.
36
What does LEFT JOIN return
All rows from the left table, plus matches from the right.
37
What does right join return
All rows from the right table, plus matches from the left.
38
What is an SQL view
A stored virtual table based on a SELECT query. It does not store its own data — it displays data from underlying tables.
39
How do you create a view
CREATE VIEW viewName AS SELECT columns FROM table WHERE condition;
40
How do you use a view?
SELECT * FROM viewName;
41
How do you delete a view?
DROP VIEW viewName;
42
What is a database index?
A structure that speeds up searches on a table, like an index in a book.
43
When should you add an index?
When you frequently search, filter, or join by a specific column.
44
What is the drawback of adding too many indexes?
Faster reads, but slower inserts/updates, because the index must be updated too.
45
How do you create an index on a column?
CREATE INDEX idx_name ON tableName(column);
46
how do you remove an index
DROP INDEX idx_name ON tableName;
47
Give an example of a subquery
SELECT * FROM employees WHERE salary > (SELECT AVG(salary) FROM employees);
48
What can a subquery in the FROM clause be used for?
SELECT * FROM (SELECT * FROM employees WHERE hourlyPay > 20) AS highEarners;
49
What does a SELECT-level subquery return?
SELECT name, (SELECT COUNT(*) FROM orders WHERE orders.customerID = customers.id) FROM customers;
50
What is a correlated subquery?
SELECT name FROM employees e WHERE salary > ( SELECT AVG(salary) FROM employees WHERE department = e.department );