Lesson 1 Flashcards

1
Q

What are the main parts of SQL?

A

DML (Data Manipulation Language) – Query and modify data (SELECT, INSERT, UPDATE, DELETE).

DDL (Data Definition Language) – Define database structure (CREATE, ALTER, DROP).

DCL (Data Control Language) – Manage access (GRANT, REVOKE).

TCL (Transaction Control Language) – Manage transactions (COMMIT, ROLLBACK).n

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

What command is used to create a database in PostgreSQL

A

CREATE DATABASE datam_test;

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

What is the SQL command to create a table?

A

CREATE TABLE classroom (
building VARCHAR(15) NOT NULL,
room_number VARCHAR(7) NOT NULL,
capacity NUMERIC(4,0) NOT NULL,
PRIMARY KEY (building, room_number)
);

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

What are integrity constraints?

A

Rules ensuring data consistency:

Primary Key – Uniquely identifies a record.

Foreign Key – Ensures referential integrity.

Not Null – Ensures a column cannot be empty.

Unique – Ensures all values are distinct.

Check – Ensures values meet a condition.

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

How do you insert data into a table?

A

INSERT INTO instructor (ID, name, dept_name, salary)
VALUES (‘102’, ‘Alice’, ‘Math’, 85000);

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

How do you select all records from a table?

A

SELECT * FROM instructor;

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

How do you retrieve unique values?

A

SELECT DISTINCT dept_name FROM instructor;

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

How do you filter records?

A

SELECT name FROM instructor WHERE dept_name = ‘Comp. Sci.’;

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

How do you sort query results?

A

SELECT name FROM instructor ORDER BY name DESC;

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

What is the SQL pattern matching operator?

A

LIKE

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

How do you find names containing “ri”?

A

SELECT name FROM instructor WHERE name LIKE ‘%ri%’;

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

What does % and _ do in LIKE queries

A

% – Matches any sequence of characters.

_ – Matches a single character.

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

What are SQL set operations?

A

UNION – Combines results (removes duplicates).
INTERSECT – Finds common elements.
EXCEPT – Finds elements in one set but not the other.

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

Example of UNION operation?

A

SELECT course_id FROM section WHERE semester = ‘Fall’ AND year = 2017
UNION
SELECT course_id FROM section WHERE semester = ‘Spring’ AND year = 2018;

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

What are the common aggregate functions in SQL?

A

AVG() – Calculates average.

SUM() – Calculates sum.

COUNT() – Counts rows.

MAX() – Finds maximum value.

MIN() – Finds minimum value.

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

How do you find the average salary in the CS department?

A

SELECT AVG(salary) FROM instructor WHERE dept_name = ‘Comp. Sci.’;

17
Q

What does GROUP BY do?

A

Groups rows based on column values for aggregation.

18
Q

What is HAVING used for?

A

Filters groups after aggregation.

18
Q

Example of grouping by department?

A

SELECT dept_name, AVG(salary) FROM instructor GROUP BY dept_name;

19
Q

Example of filtering departments with avg salary > 42000?

A

SELECT dept_name, AVG(salary) FROM instructor GROUP BY dept_name HAVING AVG(salary) > 42000;

20
Q

What is a subquery?

A

A query inside another query.

21
Q

Example of a subquery to find instructors earning more than all Biology instructors?

A

SELECT name FROM instructor WHERE salary > ALL
(SELECT salary FROM instructor WHERE dept_name = ‘Biology’);

22
Q

How do you delete all instructors from the Finance department?

A

DELETE FROM instructor WHERE dept_name = ‘Finance’;

23
Q

How do you update salaries by 5%?

A

UPDATE instructor SET salary = salary * 1.05;

24
Q

How do you increase salaries for instructors earning less than 70000?

A

UPDATE instructor SET salary = salary * 1.05 WHERE salary < 70000;

25
Q

What is the WITH clause used for?

A

Defines a temporary relation used in the main query.

26
Q

Example of using WITH to find departments with max budget?

A

WITH max_budget (value) AS (SELECT MAX(budget) FROM department)
SELECT dept_name FROM department, max_budget WHERE department.budget = max_budget.value;