SQL Basic Flashcards

1
Q

SQL and MySQL

A

SQL Structured Query Language

MySQL Database management sysytem-DBMS, other are PostgreSQL, SQLite, Oracle, There are slight differences in the syntax. Implement SQL standard

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

changing/selecting database

A

Use Database_name

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

to check what db i am using

A

Select Database_Name()

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

To drop a database:

A

DROP DATABASE <database-name>;</database-name>

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

To use a database:

A

USE <database-name>;</database-name>

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

To list available databases:

A

show databases;

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

The general command for creating a database:

A

CREATE DATABASE <database_name>;</database_name>

CREATE DATABASE soap_store;

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

2 main data types

A

numeric and string

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

show tables;

A

show columns from Table1;
DESC Table1;

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

Insert a cat into a table

A

INSERT INTO cats (name, age)
VALUES (‘Blue Steele’, 5);

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

Primary key

A

means unique,
auto-increment

cat_id INT PRIMARY KEY
at the begging or
PRIMARY KEY (cat_id)
at the end

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

Update example

A

UPDATE cats SET breed=’Shorthair’ WHERE breed=’Tabby’;

using SET and where

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

What to use before Deleting/Updating

A

Select before UPDATE/ DELETE!

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

String functions

A
  1. concat
  2. substring
  3. replace
  4. reverse
  5. char_length
  6. upper&lower

using select

SELECT CONCAT(author_fname,’ ‘, author_lname) AS author_name FROM books;
SELECT SUBSTRING(‘Hello World’, 1, 4);

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

Refining selections

A

distinc- only unique values -> Select Distinct

SELECT DISTINCT author_lname FROM books;

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

Order by example

A

SELECT author_lname FROM books ORDER BY author_lname;
ASCENDING By Default

17
Q

Limit example

A

SELECT title, released_year FROM books
ORDER BY released_year DESC LIMIT 5;

18
Q

LIKE

A

Better searching

WHERE author_fname LIKE ‘%da%’
WILDCARDS

19
Q

Aggregate functions

A

Count
Group By
Min/Max
Sum
Avg

20
Q

Count example

A

SELECT COUNT(*) FROM books;

SELECT COUNT(author_fname) FROM books;

SELECT COUNT(DISTINCT author_fname) FROM books;

SELECT COUNT(*) FROM books
WHERE title LIKE ‘%the%’;

21
Q

GROUP BY

A

GROUP BY summarizes or aggregates identical data into single rows

22
Q

Data types- String data types

A

char- fixed in size length, for text of similar length
varchar- text of different sizes
nvarchar- 1 character takes 2 bytes, max length is 50

23
Q

Data types- numeric

A

tinyint- from -127 to 128
smallint- from -32k to +32k
mediumint- from -8 milion to + 8 milion
int- from -2b to +2b

24
Q

Data types- decimal

A

decimal- (total number of digits (maxium/ up to), digits after decimal)
float- not as precise as double
double- very precise, dobule as precise

25
Q

Constraints

A

Not null
unique
Check
Alter table

26
Q

Named constraints

A

CREATE TABLE partiers2 (
name VARCHAR(50),
age INT,
CONSTRAINT age_over_18 CHECK (age > 18)
);

27
Q

Primary key, foreign key

A

Primary key- it is always unique

Foreign key- refernces to another table within another table

28
Q

INNER JOIN

A

Select all records from A and B where the join condition is met

SELECT * FROM customers
JOIN orders
ON customers.id = orders.customer_id;

29
Q

LEFT JOIN

A

Select everything from A, along with any matching records in B

30
Q

RIGHT JOIN

A

Select everything from B, along with any matching records in A

31
Q

On delete cascade

A

ON DELETE CASCADE at the end at the table so when i delete the data that are being referenced it gets deleted

32
Q

View

A

a reusable query

CREATE VIEW full_reviews AS SELECT * FROM …

33
Q

Having

A

specify condition on groups, typically formed using Group By clause

GROUP BY title HAVING COUNT(rating) > 1;

34
Q

Roll up

A

works with Group by, make a statistic in the last row-average, count…

34
Q

Modes

A

-setting of current sql

– To View Modes:
SELECT @@GLOBAL.sql_mode;
SELECT @@SESSION.sql_mode;

– To Set Them:
SET GLOBAL sql_mode = ‘modes’;
SET SESSION sql_mode = ‘modes’;

35
Q

Window Function

A

make a Grupy By function, but show the result next to every row,
it doesnt reduce the amout of rows