Tutorial 1: DATA I SQL
Selecting and Renaming columns
1. Select all data from a table
For example: Select all data from the artists table:
SELECT * FROM artists;
* Always write the statements in capital letters
* Always end your statement with ;
* Run the query with CRTL + F9
* To select all you type a *
2. Select a specific column from a table
For example: Select the column name from the tracks table:
SELECT name FROM tracks;
3. Select multiple columns from a table
For example: Select the columns name and id from the tracks table:
SELECT name, id FROM tracks;
4. Rename a column
For example: Shows the column name from tracks as song:
SELECT name AS song FROM tracks;
Select Count and Select Distinct
1. Count the number of rows in a column
For example: Count how many rows there are in the name column for artists:
SELECT COUNT(name) FROM artists;
* Place the column between brackets
2. Combining count and renaming
For example: Count how many rows there are in the name column and call it number_of_tracks:
SELECT COUNT(name) AS number_of_tracks FROM tracks;
3. Select unique values from a column
For example: Show the different countries from the table plays:
SELECT DISTINCT country FROM plays;
4. Combining count and distinct
For example: Count how many different countries there are in the table plays:
SELECT COUNT(DISTINCT country) FROM plays;