SQL Flashcards

1
Q

base command to make a new table

A

CREATE schema_name.table_name

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

command to make a new database or schema

A

CREATE database_name

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

command to delete a database or schema

A

DROP schema_name

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

add attributes syntax when creating a table

A

CREATE tablename
(attributename, datatype, DEFAULT defaultvalue);

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

constrain table column not to have null values

A

NOT NULL

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

constrain table column value to meet a condition

A

CHECK (condition)

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

constrain a table column to refuse duplicate values

A

UNIQUE or UNIQUE(attribute list)

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

set table attribute as the primary key

A

PRIMARY KEY

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

set multiple table attributes as the primary key

A

PRIMARY KEY (attribute list)

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

set foreign key for a table attribute

A

… REFERENCES schema.table(attribute)

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

modify existing values on a table

update the salary of employees in the ‘employees’ table with a department ID of 5 to 50000

A

UPDATE employees
SET salary = 50000
WHERE department_id = 5;

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

modify the structure of an existing table, such as adding, deleting, or

add a ‘birthdate’ column of type DATE to the ‘employees’ table

A

ALTER TABLE employees
ADD birthdate DATE;

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

modify the structure of an existing table, such as adding, deleting, or

remove the ‘birthdate’ column from the ‘employees’ table

A

ALTER TABLE employees
DROP COLUMN birthdate;

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

delete records from a table

delete all records from the ‘employees’ table where the department ID is 5

A

DELETE FROM employees
WHERE department_id = 5;

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

retrieve all columns from the ‘employees’ table

A

SELECT * FROM employees;

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

select specific columns from a table

select the ‘name’ and ‘email’ columns from the ‘employees’ table

A

SELECT name, email
FROM employees;

17
Q

filter rows

select all ‘employees’ with a ‘salary’ greater than 50000

A

SELECT *
FROM employees
WHERE salary > 50000;

18
Q

sort all ‘employees’ by their salary in descending order

A

SELECT *
FROM employees
ORDER BY salary DESC;

19
Q

get the top 5 highest-paid (‘salary’) ‘employees’

A

SELECT *
FROM employees
ORDER BY salary DESC
LIMIT 5;

20
Q

count the number of ‘employees’ in each department

‘department_id’

A

SELECT department_id, COUNT(*) FROM employees
GROUP BY department_id;

21
Q

find departments with more than 10 ‘employees’

‘department_id’

A

SELECT
department_id,
COUNT()
FROM employees
GROUP BY department_id
HAVING COUNT(
) > 10;

22
Q

how can I query a column that contains a space or other special character in the name?

A

put quotes around it (note that this will make it case sensitive)

23
Q

write a single line comment

A

use -- (double dash)

-- SELECT statement that will not run

24
Q

write a multi-line comment

A

/* put comment inside slash stars */