SQL Flashcards

1
Q

What is a foreign key?

A

A foreign key is a column or group of columns in a relational database table that refers to the primary key of another table.

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

What is schema?

A
  • a collection of Tables, which can be split and grouped according to logic.
  • In MySQL, Schema and Database are integrated into one.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Command to display table column info.

A

SHOW FULL COLUMNS FROM table_name;

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

Command to delete table

A

DROP TABLE table_name;

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

Delete data in table (keep the table).

A

TRUNCATE table_name;

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

what are the number data types in SQL?

A
  1. BIGINT, INT, MEDIUMINT, SMALLINT, TINYINT
  2. DOUBLE, FLOAT, DECIMAL
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What are the text types in SQL?

A

CHAR, VARCHAR (variable length –> VARCHAR(45)), can be set, TEXT, LONGTEXT

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

what are date time data types in SQL?

A

DATE, MONTH, YEAR, DATETIME , TIMESTAMP (limited to a range between 1970-01-01 00:00:01 and 2038-01-19 03:14:07)

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

Add column ‘age’ to table ‘users’ with data type INT and null values allowed. Insert after column ‘name’.

A

ALTER TABLE users
ADD COLUMN age INT NULL AFTER name;

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

Command to change existing column.

A

ALTER TABLE new_schema.users
CHANGE COLUMN id id INT(11) NOT NULL AUTO_INCREMENT,
CHANGE COLUMN name user_name VARCHAR(45) NOT NULL DEFAULT ‘No Name’;

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

Command to display all data in a table

A

SELECT * FROM <table_name>;</table_name>

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

Show all data for user with id=2

A

SELECT * FROM users WHERE id= 2;

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

What is the difference between WHERE and HAVING clauses?

A

the WHERE clause is used to filter records before they are grouped and aggregated, while the HAVING clause is used to filter groups after they have been formed.

SELECT * FROM customers
WHERE city = ‘New York’;
########
SELECT city, COUNT() as count
FROM customers
GROUP BY city
HAVING COUNT(
) > 2;

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

example query to find all occurrences of error code 403

A

SELECT system_number, DATE(date_time) as error_date, COUNT(*) as error_count
FROM table_name
WHERE error_code = 403
GROUP BY system_number, error_date;

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

Types of constraints in SQL

A

NOT NULL Constraint: This constraint ensures that a column cannot contain null values. It forces the column to have a value for every row.
UNIQUE Constraint: This constraint ensures that each value in a column is unique, meaning that no two rows can have the same value in that column.
PRIMARY KEY Constraint: This constraint is a combination of NOT NULL and UNIQUE constraints. It ensures that a column or a group of columns uniquely identifies each row in a table.
FOREIGN KEY Constraint: This constraint ensures that the values in a column or a group of columns in one table match the values in another table’s primary key column(s).
CHECK Constraint: This constraint ensures that a value in a column meets a specific condition, such as a range of values, a regular expression, or a list of allowed values.
DEFAULT Constraint: This constraint sets a default value for a column when no value is provided.

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

What is the difference between GROUP BY and ORDER BY clauses?

A

GROUP BY is used to group the result set and perform calculations on the data (typically used with aggregate functions, such as COUNT(), SUM(), AVG(), and MAX()) , while ORDER BY is used to sort the result set in a specific order.

17
Q

query that groups the result set by the “department” column and calculates the total salary for each department

A

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

18
Q

sort employees in ‘employees’ table by the “last_name” column in descending order:

A

SELECT *
FROM employees
ORDER BY last_name DESC;