SQL Notes Flashcards

1
Q

What does SQL stand for?

A

Structured Query Language

Used to interact with data in tables

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

How is SQL different to excel?

A

Unlike an excel spreadsheet, tables in a relational database can be connected to each other via unique identifiers called keys

For example a relational database could have a table for employees, departments and salaries

Each table contains a unique key for each employee

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

How to use select function?

A

SELECT <columns> FROM <table></columns>

If want to get all columns use “*”

SELECT * FROM employees;

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

How do you use the limit function?

A

SELECT * FROM employees LIMIT 5

Will give us the top 5 employees

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

How to use the ORDER BY function?

A

Used if we want a subsection of rows sorted by a specific column

ORDER BY <column> ASC, would order in ascending order and DESC is descending</column>

Text is done alphabetically, Numbers done by value

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

How would you order the first 5 employees by in ascending order by first name?

A

SELECT * FROM employees ORDER BY first_name LIMIT 5

Note that we did not include the ASC or DESC keywords here because SQL sorts in ascending order by default.

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

Addition

Subtraction

Multiplication

Division

A

+

-

*

/

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

How would you SELECT employee number and double each salary?

A

SELECT emp_no, salary, salary*2 FROM salaries;

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

Greater than, greater than equal to, equal to, less than, less than or equal to?

A

>

> =

=

<

<=

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

Example of how to use the “where function” to choose all columns from all employees, where they are males?

A

SELECT * FROM employees WHERE gender = ‘M’;

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

How could you use AND to SELECT * FROM employees WHERE gender = ‘M’; to select for those hired after January 1st 1990?

A

SELECT * FROM employees WHERE gender = ‘M’; AND hire_date > ‘1990-01-01’

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

How do you use OR?

A

Exact same concept as AND, just doesn’t have to fulfil both conditions

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

When do you use %

A

Is a wildcard, so use it with to find things similar eg.

SELECT * FROM employees WHERE first_name LIKE ‘ANN%’

Can also use it to fill missing letters eg. osc%r would give Oscar

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

How would you use COUNT to find the number of rows in the employee table?

A

SELECT COUNT(*) FROM employees;

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

How do you use MIN and MAX?

A

Same function as excel

SELECT MIN(<column) FROM table;

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

What does AVG do?

A

Exact same position as MIN or MAX just finds the average

17
Q
A