Coder Social home page Coder Social logo

dsc-more-practice-with-sql-queries-lab-online-ds-ft-051319's Introduction

More Practice With SQL Queries - Lab

Introduction

In this lesson, we'll run through some practice questions to refresh your knowledge of SQL Queries!

Objectives

You will be able to:

  • Use GROUP BY statements in SQL to apply aggregate functions like: COUNT, MAX, MIN, and SUM
  • Decide and perform whichever type of join is best for retrieving desired data
  • Use the HAVING clause to compare different aggregates
  • Write subqueries to decompose complex queries

Getting Started

As in previous labs, we'll make use of the sqlite3 library as well as Pandas. By combining them, we'll be able to write queries as Python strings, and make sure that the results are always returned as a Pandas DataFrame.

We'll start by loading both libraries and connecting to the database we'll be using for this lab, data.sqlite. You may remember this database from a previous lab. As a refresher, here's the ERD diagram for this database:

In the cell below:

  • Import the necessary libraries pandas and sqlite3
  • Establish a connection to the database data.sqlite
  • Get the cursor from the connection and store it in the variable c

Basic Queries

Now, let's review basic SQL queries. In the cell below:

Write a query that gets the first name, last name, phone number, address, and credit limit for all customers in California with a credit limit greater than 25000.00.

# For the first query, the boilerplate for getting 
# the query into a DataFrame has been provided for you
c.execute(""" """)
df = pd.DataFrame(c.fetchall())
df.columns = [x[0] for x in c.description]
df

Expected Output

Aggregate Functions and GROUP BY

Next, write a query that gets the average credit limit per state.

Expected Output

JOINs

Now, write a query that uses JOIN statements to get the customer name, customer number, order number, status, and quantity ordered. Print only the head of this DataFrame.

Expected Output

HAVING and ORDER BY

Now, return the customerName, customerNumber, productName, productCode, and total number ordered for any product a customer has bought 10 or more of cumulatively. Sort the rows in descending order by the quantity ordered.

Hint: For this one, you'll need to make use of HAVING, GROUP BY, and ORDER BY -- make sure you get the order of them correct!

Expected Output

Subqueries

Finally, get the first name, last name, employee number, and office code for employees from offices with less than 5 employees. Print the first five rows of this DataFrame.

Expected Output

Summary

In this lesson, we reviewed all the major concepts and keywords associated with SQL queries!

dsc-more-practice-with-sql-queries-lab-online-ds-ft-051319's People

Contributors

fpolchow avatar loredirick avatar mas16 avatar mathymitchell avatar mike-kane avatar sumedh10 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.