mySQL Flashcards

Introduction

1
Q

Get all columns from a table

A

SELECT *
FROM table

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

Get a column from a table

A

SELECT column
FROM table

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

Get multiple columns from table

A

SELECT col1, col2
FROM table

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

Use alias names

A

AS

SELECT col1, col2 AS col2_new
FROM table

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

Arrange the rows in asscending order of values in a column

A

ORDER BY

SELECT col1, col2
FROM table
ORDER BY col2

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

Arrange the rows in descending order of values in column

A

ORDER BY col DESC

SELECT col1, col2
FROM table
ORDER BY col2 DESC

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

Limit the number of rows returned

A

LIMIT

SELECT *
FROM table
LIMIT 2

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

Get unique values, filtering out duplicate rows, returning only unique rows.

A

DISTINCT

SELECT DISTINCT column
FROM table

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

Get rows where a number is greater than a value

A

WHERE col1 > n

SELECT col1
FROM table
WHERE col1 > value

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

Get rows where a number is greater than or equal to a value

A

WHERE col >=n
SELECT col1
FROM table
WHERE col1 >= value

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

Visualize Concatenating Columns with a New Name

A

SELECT CONCAT(first_name, ‘ ‘, last_name) AS full_name
FROM employees;

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

Visualize Using in Aggregations

A

SELECT department, SUM(salary) AS total_salary
FROM employees
GROUP BY department;

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

Get rows where a number is equal to a value

A

SELECT col1
FROM table
WHERE col1 = value

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

Get rows where a number is not equal ( WHERE col != n)

A

SELECT col1
FROM table
WHERE col1 <> value

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

Get rows where a number is between two values (inclusive)

A

SELECT col1
FROM table
WHERE col1 BETWEEN value1 AND value2

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

Get rows where text is equal to a value

A

SELECT col1, col2
FROM table
WHERE x = ‘string’

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

Get rows where text is one of several values

A

SELECT col1, col2
FROM table
WHERE x IN (‘string’, ‘string2’)

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

Get rows where text contains specific letters with WHERE col LIKE ‘%abc%’ (% represents any characters)

A

SELECT col1
FROM table
WHERE col1 LIKE ‘%abc%’

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

Get the rows where one condition and another condition holds with WHERE condn1 AND condn2

A

SELECT col1, col2
FROM table
WHERE col1 < value
AND col2 > value2

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

Get the rows where one condition or another condition holds with WHERE condn1 OR condn2

A

SELECT col1
FROM table
WHERE col1 < value
OR col2 > value

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

Get rows where values are missing with WHERE col IS NULL

A

SELECT col1, col2
FROM table
WHERE col1 IS NULL

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

Get rows where values are not missing with WHERE col IS NOT NULL

A

SELECT col1, col2
FROM table
WHERE col1 IS NOT NULL

23
Q

Get the total number of rows

A

SELECT COUNT(*)
FROM table

24
Q

Get the total value of a column

A

SELECT SUM(col)
FROM table

25
Q

Get the mean value of a column

A

SELECT AVG(col)
FROM table

26
Q

Get the maximum value of a column

A

SELECT MAX(col)
FROM table

27
Q

Get summaries grouped by values

A

SELECT col1, COUNT(*)
FROM table
GROUP BY col1

28
Q

Get summaries grouped by values, in order of summaries

A

SELECT col1, SUM(col2) AS totals
FROM table
GROUP BY col1
ORDER BY totals DESC

29
Q

Get rows where values in a group meet a criterion with GROUP BY col HAVING condn

A

SELECT col1, SUM(col2) AS totals
FROM table
GROUP BY col1
ORDER BY totals DESC
HAVING totals > value

30
Q

Filter before and after grouping with WHERE condn_before GROUP BY col HAVING condn_after

A

SELECT col1, SUM(col2) AS totals
FROM table
WHERE col3 = ‘string’
GROUP BY col1
ORDER BY totals DESC
HAVING totals > value

31
Q

Create a table

A

CREATE TABLE table_name (
column_name data_type constraints
);

32
Q

Designate a primary key

A

A. column_name data_type PRIMARY KEY
B. PRIMARY KEY (column_name)

33
Q

What are the data types?

A

Numeric, string, date/time

34
Q

Numeric datatype

A

INT
DECIMAL
FLOAT
DOUBLE
BIT

35
Q

String data

A

CHAR
VARCHAR
TEXT
JSON

36
Q

Date/Time

A

DATE
TIME
YEAR
DATETIME

37
Q

Constraints

A

Rules enforced on data to ensure accuracy, consistency, and integrity.
Primary key, Foreign key, Unique, Not Null, Default, Null, Check, Auto_Increment, Index

38
Q

Primary Key

A

Ensures that a column (or combination of columns) has unique values and cannot contain NULL values.
CREATE TABLE employees (
id INT PRIMARY KEY,
name VARCHAR(100)
);

39
Q

FOREIGN KEY

A

Establishes a relationship between two tables
CREATE TABLE orders (
order_id INT PRIMARY KEY,
customer_id INT,
FOREIGN KEY (customer_id) REFERENCES customers(id)
);

40
Q

UNIQUE

A

Ensures that all values in a column are unique but allows a single NULL value
CREATE TABLE users (
user_id INT PRIMARY KEY,
email VARCHAR(100) UNIQUE
);

41
Q

NOT NULL

A

Prevents a column from having NULL values.
CREATE TABLE products (
product_id INT PRIMARY KEY,
product_name VARCHAR(100) NOT NULL
);

42
Q

Default

A

Sets a default value for a column if no value is specified during an INSERT
CREATE TABLE orders (
order_id INT PRIMARY KEY,
order_status VARCHAR(50) DEFAULT ‘Pending’
);

43
Q

CHECK (Available in MySQL 8.0+)

A

Ensures that all values in a column meet a specified condition.
CREATE TABLE employees (
id INT PRIMARY KEY,
age INT CHECK (age >= 18)
);

44
Q

AUTO_INCREMENT

A

Automatically generates a unique number for a column (often used with PRIMARY KEY)
CREATE TABLE customers (
customer_id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(100)
);

45
Q

INDEX

A

Improves query performance by creating a fast lookup for columns. Not a direct constraint, but often used alongside UNIQUE or FOREIGN KEY

CREATE INDEX idx_email ON users (email);

46
Q

Visualize combining multiple constraints

A

CREATE TABLE marvel_superheroes (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(100) NOT NULL,
alias VARCHAR(100) NOT NULL,
first_appearance YEAR NOT NULL,
team VARCHAR(100),
active_status BOOLEAN DEFAULT TRUE
);

47
Q

Visualize insulting multiple rows

A

INSERT INTO marvel_superheroes (name, alias, first_appearance, team, active_status)
VALUES
(‘Steve Rogers’, ‘Captain America’, 1941, ‘Avengers’, TRUE),
(‘Tony Stark’, ‘Iron Man’, 1963, ‘Avengers’, FALSE),
(‘Peter Parker’, ‘Spider-Man’, 1962, ‘Avengers’, TRUE),
(‘T'Challa’, ‘Black Panther’, 1966, ‘Avengers’, TRUE),
(‘Logan’, ‘Wolverine’, 1974, ‘X-Men’, TRUE),
(‘Wanda Maximoff’, ‘Scarlet Witch’, 1964, ‘Avengers’, TRUE),
(‘Natasha Romanoff’, ‘Black Widow’, 1964, ‘Avengers’, FALSE),
(‘Scott Summers’, ‘Cyclops’, 1963, ‘X-Men’, TRUE);

48
Q

Delet data or row from table

A

DELET FROM table
WHERE column = ‘string’;

49
Q

Update data

A

UPDATE table
SET column = TRUE
WHERE column2 = ‘string’;

50
Q

WHERE clase

A

A piece of conditional logic that limits the amount of data returned. Usually found after FROM clause

51
Q

In and Not IN

A

SELECT column
FROM table
WHERE value IN (val1,val2,val3)

52
Q

Visualize how to create a table

A

CREATE TABLE {table_name} (
{column1}{datatype1}{constraint1}
{column2}{datatype2}{constraint2}
{column3}{datatype3}{constraint3}
)

53
Q

What are column constraints

A

Keywords that give special properties to columns:
NOT NULL, NULL, UNIQUE, PRIMARY KEY