Practice your SQL knowledge by writing queries on a database of Tesla data.

Source: Tesla

The Database

Open the database of Tesla data in a new tab here. The database is stored in DB Fiddle, a DBMS simulator that allows us to practice writing SQL queries without purchasing or downloading software.

  1. Factories data table: list of factories that Tesla owns
  2. Executives: information on Tesla’s executive team
  3. Financials: revenue and profit data from 2016 through 2020

Question 1: Write a query that displays the Cars data table

This question requires a simple SELECT command that calls all rows from the Cars data table. To run a query in DB Fiddle, type the SQL code in the ‘Query SQL’ section of and then click ‘Run’ in the top left corner of the page. Here is the code to run this SQL query:

SELECT * FROM Cars
Source: Tesla

Question 2: Write a query that displays both the Cars data table and the Factories table

To display multiple tables, we need to write two different queries. Note that to separate queries, a semicolon is needed after each query except for the last one.

SELECT * FROM Cars;
SELECT * FROM Factories
Source: Tesla

Question 3: Write a query that displays only the factory name and city columns from the Factories table

The asterisk in the first two questions is used to tell the database to display all columns from that table. We also have the ability in SQL to display only certain rows from a data table. When a data table has hundreds of columns, it is helpful to only select the columns needed.

SELECT Name, City FROM Factories
Source: Bloomberg

Question 4: Write a query that sorts Tesla executives from highest to lowest salary

Let’s introduce our second command, ORDER BY. The ORDER BY command in SQL allows us to sort data. Similar to sorting data in a spreadsheet, we can sort data in SQL.

SELECT * FROM Executives
ORDER BY Salary DESC
Source: Financial Times

Question 5: Write a query that displays Tesla executives who are older than 45 years old

We learned the ORDER BY command will sort in SQL — so how do we filter data in a database?

SELECT * FROM Executives
WHERE Age > 45
Source: Elon Musk

Question 6: Write a query that displays all years for which Tesla made a profit

This question is another example of using the WHERE command except we now want to use it on the Financials data table.

SELECT * FROM Financials
WHERE Profit > 0
Source: Tesla

Question 7: Write a query that displays all years in which Tesla made a profit OR made more than $20 billion in revenue

This question involves learning a new set of SQL commands. The AND, OR, and NOT commands are commonly used with the WHERE command when we want to use multiple filters in one query,

SELECT * FROM Financials
WHERE Profit > 0 OR Revenue > 20000
SELECT * FROM Financials
WHERE Profit > 0 AND Revenue > 20000
Source: Tesla

Question 8: How much revenue did Tesla make from 2016 through 2020?

The next set of questions will use one of three SQL functions: SUM, AVERAGE, and COUNT. These functions allow us to perform calculations in SQL.

SELECT SUM(Revenue) FROM Financials
Source: Tesla

Question 9: What was Tesla’s average profit over the past five years?

The second function we want to cover is the AVG function, which stands for average. This function will calculate the average of a column of integers.

SELECT AVG(Profit) FROM Financials
Source: Tesla

Question 10: How many factories does Tesla have?

The last function we will cover is the COUNT function. This function can count how many rows of data are in a column. To find out how many factories are in the factories data table, we use the COUNT function on any column name in the factories table.

SELECT COUNT(Name) FROM Factories
Source: space.com

Want to learn more SQL?

The best way to learn is to practice. Try to ask your own questions using this dataset and attempt to solve them by writing SQL queries.

The best way for business students to learn to code.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store