SQL Flashcards

(22 cards)

1
Q

How do you use an INNER JOIN in SQL?

A

SELECT … FROM table1
INNER JOIN table2
ON table1.pk = table2.fk;

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

How do you connect MySQL to Python?

A

import mysql.connector
mydb = mysql.connector.connect(
host=”localhost”,
user=”annaho”,
passwd=”annahofs”)

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

How do you create a cursor and use it (Python)?

A

mycursor = mydb.cursor()

mycursor.execute(“SQL QUERY”)

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

How do you close the connection (Python)?

A

mycursor.close()
mydb.close()

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

How do you fetch data from the DB (Python)?

A

mycursor.fetchall()
mycursor.fetchone()
mycursor.fetchmany(size) #number of items to fetch

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

How do you INSERT data into SQL (Python)?

A

mycursor.execute(“INSERT INTO table_name VALUES (%s, %s , … , %s)”, (data1, data2, …, datan))

%s are placeholders
mydb.commit()

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

How do you use a LEFT/ RIGHT JOIN in SQL?

A

SELECT * FROM table_1
LEFT (RIGHT) JOIN table_2
ON table_1.pk = table_2.fk;

Left join returns all rows from first table with matches from second table
Right - vice versa

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

How do you use the UNION operator in SQL?

A

Use UNION operator between two select statements.
Returns all distinct rows.

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

How do you use a FULL JOIN in SQL?

A

SELECT * FROM table_1
FULL JOIN table_2
ON table_1.pk = table_2.fk;

All values from table 1 and table 2

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

How to INSERT into SQL?

A

INSERT INTO table_name
VALUES (attribute_value1, …, attribute_valuen);
Rows must be complete; use NULL for empty rows.

INSERT INTO table_name (attribute1, …, attributen)
VALUES (attribute_value1, …, attribute_valuen);

For auto inc pk: NULL

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

How to UPDATE data in SQL?

A

UPDATE table_name
SET attribute_target = expression1
WHERE attribute_select = expression2;

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

How do you DELETE data in SQL?

A

DELETE FROM table_name
WHERE attribute_name = expression;

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

What functions exist in SQL?

A

AVG(column)
COUNT(column) # returns no of rows without NULL
COUNT(*) # number of rows selected
MAX(column)
MIN(column)
SUM(column)

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

Can you use an aggregate function in a where command?

A

NO

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

How does HAVING work in SQL?

A

Used instead of WHERE for aggregate function conditions.

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

How do you use a subquery in SQL?

A

SELECT * FROM table1 WHERE attributeX {= | IN}
(SELECT attributeX
FROM table2
WHERE search_condition)

17
Q

How do you create a View in SQL?

A

Create View Parking AS
SELECT Name, Telephone, Vehicle, Registration_ Number
FROM Employee, Parking_authorization
WHERE Employee.EID= Parking_authorization.EID;

18
Q

How do you delete a view?

A

DROP VIEW view_name

19
Q

How do you add a table in SQL?

A

ALTER TABLE customer ADD Lastorder datetime;

20
Q

How do you create a trigger in SQL?

A

create trigger neworder
after insert on orders
for each row
update customer set Lastorder = now()
where customer.customer_id = new.customer_id;

21
Q

How do you create a stored procedure in SQL?

A

CREATE PROCEDURE sp_mysp()
BEGIN
SELECT avg(salary) AS avgsal FROM employee;

update employee set salary = salary * 1.1 where empid < 3;

SELECT avg(salary) AS avgsal FROM employee; END;
22
Q

How do you call a stored procedure in SQL?

A

call sp_mysp();