SQL TUTORIALS

SQL Tutorials: Intro to SELECT Statements

%UPDATE% Picture of the word python written on a stick note.

DISCLAIMER: ANSI SQL (The SQL that's used universally) includes everything taught in this section. However, some of the syntax shown will vary between your Relational Database Management Service (RDBMS).

Also, this section is intended for individuals who have at least a basic knowledge of how relational databases work.


SELECT statements are the most common statements in SQL and are used to execute queries on tables and views (we cover those in a later tutorial). To get started with SELECT statements, we'll cover the following learning objectives:

  • SELECT Statement Syntax
  • WHERE Clause
  • GROUP BY and HAVING Clauses
  • ORDER BY Clause

What Are the Components of a SELECT Statement?




Summary:

  • If you want to get all the data from a table, use the following syntax:
    SELECT *
    FROM [table_name];
  • If you want to see how many records are in a table, use the following syntax (we will go over more aggregate functions in a later tutorial):
    SELECT COUNT(*)
    FROM [table_name];
  • If you only want to get data from specific columns in a table, use the following syntax:
    SELECT [col1], [col2], [col3], [col4]
    FROM [table_name];
  • If you want to filter the rows returned by your query, use the WHERE clause with the following syntax:
    SELECT *
    FROM [table_name]
    WHERE [condition(s)];
  • If you want to filter the rows returned by your query using multiple conditions, use the AND and/or OR keywords with the following syntax:
    SELECT *
    FROM [table_name]
    WHERE [condition1] [AND/OR] [condition2];
  • If you want to sort the rows returned by your query in ASCENDING order, use the ORDER BY clause with the following syntax:
    SELECT *
    FROM [table_name]
    ORDER BY [column1];
  • If you want to sort the rows returned by your query in DESCENDING order, use the ORDER BY clause, in conjunction with the DESC keyword, with the following syntax:
    SELECT *
    FROM [table_name]
    ORDER BY [column1] DESC;
  • If you want to restrict the number of rows returned by your query, use the LIMIT keyword, followed by the number of rows to return, with the following syntax:
    SELECT *
    FROM [table_name]
    ORDER BY [column1]
    LIMIT [num_rows];
  • NOTE: An Alternative Key is a column in the table that could also be used as the Prumary Key.
  • NOTE: A Result Set is the temporary table returned by your query.

Exercise

Congratulations! You just completed the Intro to SQL Statements Tutorial! To help test your knowledge, let's use the database you populated in the previous tutorial to run some basic queries. This exercise is based on the exercise of the previous tutorial.
**It's highly recommended that you complete the exercise outlined in the previous tutorial before beginning this exercise.**

Instructions:

  1. Open your RDBMS Client (e.g., PostgreSQL, MySQL, MS SQL Server).
  2. Download the earthquake.csv File from Socratica's GitHub Repo.
  3. Import the CSV into a table called "earthquakes" (see links below for how to do this in your respective RDBMS Client):
  4. Using the newly created "earthquake" table, answer the following questions:
    1. How Many Rows are in the "earthquake" Table?
    2. How Many Eathquakes occurred before 1/1/2010?
    3. When was the First Earthquake Recorded?
    4. Where did the Strongest Earthquake take place?
    5. What are the Magnitudes for the 10 Weakest Earthquakes in the 1900s?
  5. Exercise Completed! Click here to view the answers.
  6. Have any issues with the above exercise? Post your question on Discord!