MYSQL basic Flashcards

1
Q

What is SQL?

A

SQL (Structured Query Language) is a programming language used to manage and manipulate relational databases. It’s used to perform tasks such as querying data, updating records, and creating or modifying database structures.

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

What is a primary key?

A

A primary key is a unique identifier for each record in a table. It ensures that no two rows have the same key and does not allow NULL values.

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

What is the difference between DELETE and TRUNCATE?

A

DELETE removes specific rows based on a condition, can be rolled back, and triggers events. TRUNCATE removes all rows in a table, is faster, cannot be rolled back, and does not trigger events.

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

Explain the SELECT statement and its use.

A

The SELECT statement is used to query data from a database. It specifies the columns to retrieve and can include conditions for filtering the data.

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

What is a foreign key?

A

A foreign key is a field (or a set of fields) in one table that uniquely identifies a row of another table. It creates a relationship between two tables.(non primary of one table but primary key of another table)

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

How does a JOIN work in SQL and types of JOIN’s?

A

JOIN is used to combine rows from two or more tables based on a related column between them. Types include INNER JOIN, LEFT JOIN, RIGHT JOIN, and FULL JOIN.

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

Describe the difference between INNER JOIN and LEFT JOIN.

A

INNER JOIN returns only the rows where there is a match in both tables. LEFT JOIN returns all rows from the left table and matching rows from the right table; non-matching rows from the right table result in NULL.

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

What does the WHERE clause do?

A

The WHERE clause filters records to include only those that meet specific conditions. It is used in SELECT, UPDATE, and DELETE statements

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

How do you sort results in SQL?

A

Use the ORDER BY clause to sort results in ascending (ASC) or descending (DESC) order based on one or more columns.

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

What is GROUP BY used for?

A

The GROUP BY clause groups rows that have the same values in specified columns. It’s often used with aggregate functions like COUNT, SUM, AVG, etc.

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

What is the purpose of the HAVING clause?

A

HAVING filters records after aggregation (using functions like SUM or COUNT), while WHERE filters rows before aggregation.

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

Describe the UPDATE statement.

A

The UPDATE statement modifies existing records in a table based on specified conditions. eg
UPDATE students
SET email = ‘new_email@example.com’
WHERE student_id = 101;

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

What is a subquery?

A

A subquery is a query nested within another query. It can return data that the outer query will use.

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

What is a CHECK constraint?

A

A CHECK constraint limits the values that can be placed in a column by enforcing a condition that each row must meet. used during creation of table columns

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

Explain the UNIQUE constraint.

A

The UNIQUE constraint ensures that all values in a column are distinct from each other, preventing duplicate values.

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

Types of SQL commands

A

DML (Data Manipulation Language)
DDL (Data Definition Language)
DCL (Data Control Language)
TCL (Transaction Control Language)
DQL (Data Query Language)

15
Q

Explain DML and it’s commands

A

DML (Data Manipulation Language)
Used to manipulate data within existing tables.
Commands:
SELECT: Retrieve data from a database.
INSERT: Insert new records into a table.
UPDATE: Modify existing records in a table.
DELETE: Delete records from a table.

16
Q

Explain DDL and it’s commands

A

Used to define, modify, or remove the structure of database objects like tables, indexes, etc.
Commands:
CREATE: Create new database objects (e.g., tables, indexes).
ALTER: Modify existing database objects.
DROP: Delete database objects.
TRUNCATE: Remove all records from a table without logging individual row deletions.

17
Q

Explain DCL and it’s commands

A

DCL (Data Control Language)
Used to control access to data in the database.
Commands:
GRANT: Provide specific privileges to users.
REVOKE: Remove previously granted privileges.

18
Q

Explain TCL and it’s commands

A

TCL (Transaction Control Language)
Used to manage transactions in the database to maintain integrity.
Commands:
COMMIT: Save the current transaction’s changes permanently.
ROLLBACK: Undo changes made in the current transaction.
SAVEPOINT: Set a save point within a transaction to roll back to if needed.
SET TRANSACTION: Set transaction characteristics such as isolation level.

19
Q

Explain DQL and it’s commands

A

DQL (Data Query Language)
Sometimes considered a subset of DML, DQL includes only the SELECT command used specifically for querying the database.
Commands:
SELECT: Retrieve data from tables based on specific criteria.

20
Q

What is an INNER JOIN?

A

An INNER JOIN returns records that have matching values in both tables.
Example:
SELECT students.first_name, enrollments.course_id
FROM students
INNER JOIN enrollments ON students.student_id = enrollments.student_id;

21
Q

Describe a LEFT JOIN.

A

A LEFT JOIN returns all records from the left table and matching records from the right table. If no match is found, NULL values are returned for columns from the right table.
Example:
SELECT students.first_name, enrollments.course_id
FROM students
LEFT JOIN enrollments ON students.student_id = enrollments.student_id;

22
Q

What is a FULL OUTER JOIN?

A

Answer: A FULL OUTER JOIN returns all records when there is a match in either the left or right table. If there’s no match, NULL values are returned.
Note: Not all databases support FULL OUTER JOIN.

23
Q

What is the purpose of an INDEX?

A

An INDEX is used to speed up the retrieval of rows from a table. It works like a pointer and can improve performance on large datasets.

24
Q

Explain the difference between UNIQUE and PRIMARY KEY constraints.

A

Both constraints ensure unique values in a column. However, a PRIMARY KEY is the main identifier for a record and cannot contain NULL values, while a UNIQUE constraint allows one NULL value.

25
Q

What is the purpose of a DEFAULT constraint?

A

A DEFAULT constraint assigns a default value to a column if no other value is provided during record insertion.

26
Q

What are some common SQL data types?

A

Common data types include:
INT: Integer values.
VARCHAR(size): Variable-length string.
DATE: Date values.
BOOLEAN: True or false.
DECIMAL(p, s): Decimal numbers with precision and scale.

27
Q

How can you concatenate strings in SQL?

A

You can concatenate strings using the CONCAT function or the || operator (database-specific).
Example:
SELECT CONCAT(first_name, ‘ ‘, last_name) AS full_name FROM students;