SQL Flashcards

You may prefer our related Brainscape-certified flashcards:
1
Q

Returning first 10 rows in descinding order of Col_A from a table in SQLite filtered for when Col_A or Col_B are less than zero

A
SELECT *
FROM Table_Name
WHERE Col_A <0
     OR Col_B <0
ORDER BY Col_A DESC
LIMIT 10;
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Returning a count of rows in a table

A
SELECT COUNT(Column_Name)
FROM Table_Name;
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Returning rows that match a list of values

A

SELECT *
FROM table
WHERE
column_name IN (‘value1’, ‘value2’, value3, ‘value4’);

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

Creating a view

A

CREATE VIEW View_name AS

SELECT * FROM Table_name;

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

Delete a view

A

DROP VIEW View_Name;

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

Append two select statements

A

[select_statement_one]
UNION
[select_statement_two];

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

Selecting rows that occur in both SELECT statements

A

[select_statement_one]
INTERSECT
[select_statement_two];

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

Selecting rows that occur in the first SELECT statement but not the second SELECT statement

A

[select_statement_one]
EXCEPT
[select_statement_two];

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

Launching the SQLite shell

A

sqlite3 chinook.db

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

Activate column headers in SQLite shell

A

.headers on

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

Activate column formatting for SQLite shell output

A

.mode column

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

Displaying help text in SQLite shell

A

.help

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

Display the list of tables and view of a database from SQLite shell

A

.tables

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

Run a BASH command from within SQLite shell

A

.shell [BASH_Command]

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

View a table’s schema from within SQLite shell

A

.schema [Table_Name]

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

Terminate the SQLite shell

A

.quit

17
Q

Creating a table with a primary key and foreign key

A
CREATE TABLE [table_name] (
     [pk_id_name]  INTEGER PRIMARY KEY,
     [fk_id_name] INTEGER,
     [column3_name] [column3_type],
     FOREIGN KEY ([fk_id_name]) REFERENCES [fk_table]([fk_id_name])
);
18
Q

Creating a table with a compound primary key and multiple foreign keys

A

CREATE TABLE [table_name] (
[pk1_id_name] INTEGER,
[pk2_id_name] INTEGER,
[fk1_id_name] INTEGER,
[column3_name] [column3_type],
PRIMARY KEY ([pk1_id_name], [pk2_id_name]),
FOREIGN KEY ([fk1_id_name]) REFERENCES fk1_table,
FOREIGN KEY ([fk2_id_name]) REFERENCES fk2_table
);

19
Q

Insert a new row of values into certain columns of an existing table

A
INSERT INTO [table_name] (
     [column1_name],
     [column2_name],
     [column3_name]
) VALUES (
     [value1],
     [value2],
     [value3]
);
20
Q

Insert multiple rows for all columns of an existing table

A

INSERT INTO [table_name]
VALUES
([A1], [A2], [A3]),
(B1], [B2], [B3]);

21
Q

Deleting selected rows from a table

A

DELETE FROM [table_name]

WHERE [filter_expression];

22
Q

Create a new column in an existing table

A

ALTER TABLE [table_name]

ADD COLUMN [column_name] [column_type];

23
Q

Changing values for existing rows

A
UPDATE [table_name]
SET 
     [column1] = [expression],
     [column1] = [expression]
WHERE [expression];
24
Q

Import Python module for working with SQLite 3

A

import sqlite3

25
Q

Use Python to create a connection to a SQLite database

A

conn = sqlite3.connect(“job.db”)

26
Q

Create a Python Cursor class

A

cursor = conn.cursor()

27
Q

Run a SQL query from a Python cursor

A

cursor.execute(“SQL statement as string;”)

28
Q

Fetch the full results as a list of tuples from a Python cursor

A

results = cursor.fetchall()

29
Q

Fetch the one results from a Python cursor and then another

A
first_result = cursor.fetchone()
second_result = cursor.fetchone()
30
Q

Fetch the first five results from a Python cursor

A

five_results = cursor.fetchmany(5)

31
Q

Terminate a Python SQLite3 connection

A

conn.close()