SQL-Basics: Relations

As the Junior Data Scientist of Knight Industries we created a table to keep track of all our operatives: SQL-Basics: Create, Read, Update, Delete

Devon asks us to keep track of our operations aka missions as well. For the first implementation let us assume that a mission has one operative and an operative can participate in multiple missions. that’s what we call an 1 to many relationship.

  +------------+                 +----------+
  |            |1   active in   n|          |
  | operative  +-----------------+ missions |
  |            |                 |          |
  +------------+                 +----------+

Missions have an id, a code name like “Phantom Liberty”, an operative id and a total cost. Continue reading “SQL-Basics: Relations”

SQL-Functions

In SQL-Basics

we learned the standard SQL statements to create a table, insert data into it, retrieving data from tables and altering data. Now we want to do even

Devon is a data driven man and to evaluate our mission, he likes to know from us:

    • how many missions we finished
    • the total cost of all missions
    • the mission with the lowest cost
    • the mission with the highest cost
    • the average mission cost
    • the five most expensive missions

 

SELECT COUNT(*) FROM missions;
SELECT SUM(total_cost) FROM missions;
SELECT MIN(total_cost) FROM missions;
SELECT MAX(total_cost) FROM missions;
SELECT AVG(total_cost) FROM missions;
SELECT * FROM missions ORDER BY total_cost DESC LIMIT 5;

SQL-Basics: Create – Read – Update – Delete

This episode is about the basic statements needed to create, read, update and delete data in a database system.

Let’s assume we work as a data scientist for Knight Industries.  We want to help the Foundation of Law and Government to keep track of our operatives.

We decide to use a classic relational database management system or RDBMS. In order to explore Database Management Systems we can either install one locally or we can use an online tool like SQLFiddle.

To interact with RDBMS we use SQL – the Structured Query Language.

As the name says SQL (speak either S-Q-L or Sequel) is used to write structured queries. Think of “conversations” when You think of “queries”.

So, let’s fire up SQLFiddle. Continue reading “SQL-Basics: Create – Read – Update – Delete”

SQL – the dark side

Sometimes your RDBMS does not allow You certain changes like updating a table without using a WHERE that uses a key column.

When You are really sure what You want to do:

SET SQL_SAFE_UPDATES = 0;