Select data from SQL table based on multiple conditions


The following example uses ipython-sql. To install:
pip install ipython-sql

%sql is a Jupyter magic command and can be ignored when not using Jupyter notebooks.

Configure SQL for Jupyter notebook

#Load sql
%load_ext sql
#connect to sqlite
%sql sqlite://

Create a table

%%sql

CREATE TABLE players3 (first_name, last_name, age);
INSERT INTO players3 (first_name, last_name, age) VALUES
('Lebron', 'James', 33),
('Steph', 'Curry', 30),
('Mike', 'Jordan', 55),
('Mike', 'Bibby', 40);

Select records from SQL table based on multiple conditions

%%sql

--Return records where age < 40 and last_name = 'James'
SELECT
*
FROM players3
WHERE age < 40 AND last_name = 'James'
first_name last_name age
Lebron James 33


Ace your next data science interview

Get better at data science interviews by solving a few questions per week



Find a bug? Submit a suggested change on Github, or message me on Twitter.