SQL statements Flashcards

1
Q

What is used to make a db

A

CREATE DATABASE IF NOT EXISTS mydatabase

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

Show your dbs

A

SHOW DATABASES

for x in cursor

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

Make a table

A

CREATE TABLE customers (name VARCHAR(255), address VARCHAR(255))

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

Show your tables

A

SHOW TABLES

for x in cursor

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

Make a table and key

A

CREATE TABLE customers (id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(255), address VARCHAR(255))

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

Add prim keys to your table

A

ALTER TABLE customers ADD COLUMN id INT AUTO_INCREMENT PRIMARY KEY

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

Add a record

A

INSERT INTO customers (name, address) VALUES (%s, %s)
cursor.execute(sql,val)

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

Show number of rows last affected by execute

A

mycursor.rowcount

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

Save changes

A

connection.commit

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

ADd multiple rows

A

val = [()]

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

Show latest record id inserted

A

mycursor.lastrowid

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

Print all records from table

A

result = mycursor.fetchall()

for x in result

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

Print name and addresses only from your table

A

SELECT name, address FROM customers

result = cursor.fetchall()
for x in result

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

Get first row and print

A

SELECT * FROM customers

result = cursor.fetchone()

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

Get addresses that have ‘simpson st” as address

A

SELECT * FROM customers WHERE address=’simpson st’
result = cursor.fetchall()

for x in myresult

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

Get addresses that contain the word ‘way’

A

SELECT * FROM customers WHERE address LIKE=”%way%”

result=cursor.fetchall()

for x in result

17
Q

Sort everything by name in ascending order

Do it for descending

A

SELECT * FROM customers ORDER BY name

ORDER BY name DESC

18
Q

Delete records with address ‘mountain 21’

How do you delete everything

A

DELETE FROM customers WHERE address=’Mountain 21”

DELETE FROM customers

19
Q

Delete a table

A

DROP TABLE IF EXISTS customers

20
Q

EDIT address Canyon 123 to Valley 123

A

UPDATE customers SET address=”Canyon 123” WHERE address=’Valley 123’

21
Q

Get first 5 records

A

SELECT * FROM customers LIMIT 5

22
Q

Get 5 records starting from record 3

A

SELECT * FROM customers LIMIT 5 OFFSET 2

23
Q

You have two tables
One contains users names and a number that correlates to another table that contains favorite icecreams by an id number.

How do you combine fav and id?

A

import mysql.connector

mydb = mysql.connector.connect(
host=”localhost”,
user=”yourusername”,
password=”yourpassword”,
database=”mydatabase”
)

mycursor = mydb.cursor()

sql = “SELECT \
users.name AS user, \
products.name AS favorite \
FROM users \
INNER JOIN products ON users.fav = products.id”

mycursor.execute(sql)

myresult = mycursor.fetchall()

for x in myresult:
print(x)

24
Q

What’s the difference between INNER JOIN, LEFT JOIN, RIGHT JOIN

A

INNER JOIN only shows the records where there is a match.

If you want to show all users, even if they do not have a favorite product, use the LEFT JOIN statement:

RIGHT JOIN If you want to return all products, and the users who have them as their favorite, even if no user have them as their favorite, use the RIGHT JOIN statemen

25
Q

Select all users and their favorite product

A

sql = “SELECT \
users.name AS user, \
products.name AS favorite \
FROM users \
LEFT JOIN products ON users.fav = products.id”