GithubHelp home page GithubHelp logo

api-sql-intro's Introduction

SQL Intro

In this exercise you are going to practice write SQL for CREATE TABLE, INSERT and SELECT statements. Some examples have been provided for you in the sql/products folder.

Learning Objectives

  • Create a database
  • Explain that each record should have an identifier known as a primary key
  • Use a tool to get visibility on the state of the database
  • Use SQL to create and drop a table in the database
  • Explain that a table has rows and columns also known as records and fields
  • Use SQL to insert and records in a table

Set Up

Sign up to Elephant SQL and create a new database instance.

Standard Criteria

Write a SQL CREATE TABLE statement that creates a table to store films. The table should have the following columns:

  • A unique film id (this should be the primary key, use SERIAL to make it increment automatically)
  • A unique title
  • A genre
  • A release year (make this integer data type)
  • A score out of 10 (make this integer data type)

Write SQL INSERT statements to add the following records in to the films table.

title, genre, release_year, score
The Shawshank Redemption, Drama, 1994, 9
The Godfather, Crime, 1972, 9
The Dark Knight, Action, 2008, 9
Alien, SciFi, 1979, 9
Total Recall, SciFi, 1990, 8
The Matrix, SciFi, 1999, 8,
The Matrix Resurrections, SciFi, 2021, 5
The Matrix Reloaded, SciFi, 2003, 6
The Hunt for Red October, Thriller, 1990, 7
Misery, Thriller, 1990, 7
The Power Of The Dog, Western, 2021, 6
Hell or High Water, Western, 2016, 8
The Good the Bad and the Ugly, Western, 1966, 9
Unforgiven, Western, 1992, 7

Write SQL SELECT statements to return films matching the below criteria:

  • All films
  • All films ordered by rating descending
  • All films ordered by release year ascending
  • All films with a rating of 8 or higher
  • All films with a rating of 7 or lower
  • films released in 1990
  • films released before 2000
  • films released after 1990
  • films released between 1990 and 1999
  • films with the genre of "SciFi"
  • films with the genre of "Western" or "SciFi"
  • films with any genre apart from "SciFi"
  • films with the genre of "Western" released before 2000
  • films that have the world "Matrix" in their title

For the last query, you will need to research the LIKE keyword in postgres.

Extension 2

Write a SQL SELECT statements to:

  • Return the average film rating
  • Return the total number of films
  • Return the average film rating by genre

For these, you will need to research COUNT, AVG and GROUP BY keywords in postgres.

Extension 3

Create a new directors table. Each director should have a name and a unique director id. Recreate your films table and add a directorId column. Insert a few director records (the data does not need to be real, you can just make directors up). Re-insert your film data, updating each film with have a directorId. Using a SQL JOIN, write a SELECT statement that returns a list of films with their director.

Extension 4

Write a SQL SELECT statement that returns a lists of directors along with the number of films they have directed.

api-sql-intro's People

Contributors

mikemherron avatar nicopicchio 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.