This is a continuation of my work on the codeacademy data science independant project #1: Watching the Stock Market. My first post is here.


In this project, the basic tasks are:

  1. What are the distinct stocks in the table?
  2. Query all data for a single stock. Do you notice any overall trends?
  3. Which rows have a price above 100? between 40 to 50, etc?
  4. Sort the table by price. What are the minimum and maximum prices?

Setup

I write this blog using rmarkdown and the entire project is powered by bookdown. SO there’s some setup that needs to take place to get it all to work.

library(DBI)
db = dbConnect(RSQLite::SQLite(), dbname = "../../data/stocks/stocks.db")

What are the distinct stocks in the table?

SELECT DISTINCT name
FROM stocks;
Table 1: 5 records
name
Apple
Boeing
Electronic Arts
Spotify
NVIDIA

In my last post about putting together the database, I said that I was collecting data on stocks for Apple, Boeing, Spotify, EA, and Nvidia. Here’s the proof!

Which rows have a price above 100? between 40 to 50, etc?

This just requires some simple filtering!

SELECT COUNT(*) 
FROM stocks
WHERE price > 100;
Table 2: 1 records
COUNT(*)
255

Wow! That’s a few stocks!

SELECT COUNT(*) 
FROM stocks
WHERE price >= 40
  AND price <= 50;
Table 3: 1 records
COUNT(*)
0

Well, I picked pricy stocks…

Sort the table by price. What are the minimum and maximum prices?

These are the least expensive stocks

SELECT *
FROM stocks
ORDER BY price;
Table 4: Displaying records 1 - 10
symbol name date price
EA Electronic Arts 7/19/19 87.50
EA Electronic Arts 7/22/19 88.14
EA Electronic Arts 7/23/19 88.22
EA Electronic Arts 7/29/19 88.87
EA Electronic Arts 7/25/19 89.16
EA Electronic Arts 7/18/19 89.19
EA Electronic Arts 7/26/19 89.51
EA Electronic Arts 7/16/19 89.55
EA Electronic Arts 7/17/19 89.70
EA Electronic Arts 7/24/19 89.78

And these are the most expensive stocks.

SELECT *
FROM stocks
ORDER BY price DESC;
Table 5: Displaying records 1 - 10
symbol name date price
BA Boeing 7/19/19 377.3600
BA Boeing 4/30/19 375.5325
BA Boeing 6/26/19 374.9400
BA Boeing 6/20/19 374.8800
BA Boeing 5/1/19 374.6476
BA Boeing 5/3/19 374.3095
BA Boeing 6/24/19 373.9900
BA Boeing 6/18/19 373.9600
BA Boeing 5/2/19 373.6533
BA Boeing 7/22/19 373.4200