SQL Cheat Sheet
SQL Cheat Sheet
SQLite
Descriptio
Topic Syntax Example
n
Create a
new
database
and open a
database
connection
to allow
sqlite3 to
work with 1. 1
it.
2. 2
Call sqlite3
.connect() t 1. import sqlite3
connec sqlite3.con
o create a
t() nect() 2. con =
connection
sqlite3.connect("INSTRUC
to the
TOR.db")
database
INSTRUCTO Copied!Wrap Toggled!
R.db in the
current
working
directory,
implicitly
creating it if
it does not
exist.
The execut
e method
in Python's
SQLite
library
allows to
perform
SQL
commands,
including
retrieving
data from a
table using 1. 1
a query like
1. cursor_obj.execute('''inse
"Select *
execut cursor_obj.e rt into INSTRUCTOR
from
e() xecute() values (1, 'Rav', 'Ahuja',
table_name
'TORONTO', 'CA')''')
." When you
execute this Copied!Wrap Toggled!
command,
the result is
obtained as
a collection
of table
data stored
in an
object,
typically in
the form of
a list of
lists.
1. 1
2. 2
3. 3
The fetchal
l() method 4. 4
in Python
5. 5
retrieves all
the rows 1. statement = '''SELECT *
fetchall cursor_obj.f from the FROM INSTRUCTOR'''
() etchall() result set of
2. cursor_obj.execute(state
a query and
ment)
presents
them as a 3. output_all =
list of cursor_obj.fetchall()
tuples.
4. for row_all in output_all:
5. print(row_all)
Copied!Wrap Toggled!
read_sql_q
uery() is a
function
provided by
the Pandas
library in
Python, and
it is not
specific to
MySQL. It is 1. 1
a generic
read_s 1. df =
read_sql_qu function
ql_quer pd.read_sql_query("select
ery() used for
y() * from instructor;", conn)
executing
SQL queries Copied!Wrap Toggled!
on various
database
systems,
including
MySQL, and
retrieving
the results
as a Pandas
DataFrame.
seaborn.ba
rplot() is a
function in
the Seaborn
Python data
visualizatio
n library
used to
create a bar
plot, also
1. 1
known as a
seaborn.bar
bar chart. It 2. 2
plot(x="x-
is
axis_variabl 1. import seaborn
barplot particularly
e", y="y-
() used to 2. seaborn.barplot(x='Test_
axis_variabl
display the Score',y='Frequency',
e",
relationship data=dataframe)
data=data)
between a
Copied!Wrap Toggled!
categorical
variable
and a
numeric
variable by
showing the
average
value for
each
category.
read_cs df = read_csv() 1. 1
v() pd.read_csv( is a function
2. 2
'file_path.cs in Python's
v') Pandas 1. import pandas
library used
2. df =
for reading
pandas.read_csv('https://
data from a
data.cityofchicago.org/res
Comma-
Separated
Values
(CSV) file
and loading
it into a
Pandas
DataFrame. ource/jcxq-k9xf.csv')
It's a Copied!Wrap Toggled!
common
method for
working
with tabular
data stored
in CSV
format
read_sql()
is a function
provided by
the Pandas
library in
Python for
executing
SQL queries
and
1. 1
retrieving
the results 2. 2
into a
df = 1. selectQuery = "select *
DataFrame
read_s pd.read_sql( from INSTRUCTOR"
from an
ql() sql_query,
SQL 2. df =
conn)
database. pandas.read_sql(selectQu
It's a ery, conn)
convenient
Copied!Wrap Toggled!
way to
integrate
SQL
database
interactions
into your
data
analysis
workflows.
Db2
Descriptio
Topic Syntax Example
n
Copied!Wrap Toggled!
ibm_db.exe
c_immediat
e() is a
Python
function
provided by
1. 1
the ibm_db
library, 2. 2
which is
3. 3
used to
execute an 1. # Lets first drop
SQL the table
sql_statement =
statement INSTRUCTOR in
"SQL statement
immediately case it exists from
goes here"
exec_i without the a previous
stmt =
mmedi need to attempt.
ibm_db.exec_imme
ate() prepare or
diate(conn, 2. dropQuery =
bind it. It's
sql_statement) "drop table
commonly
INSTRUCTOR"
used for
executing 3. dropStmt =
SQL ibm_db.exec_imm
statements ediate(conn,
that don't dropQuery)
require
Copied!Wrap Toggled!
input
parameters
or don't
need to be
prepared in
advance.
Author(s)