1. Knowledge Hub
  2. Data Manipulation
  3. SQL basics for Data Manipulation

Explaining SELECT Statement

In this article, we will walk you through what is the SELECT Statement in SQL and how to use it to fetch data.

 

Before: Review this introductory article:
- Getting started with SQL for Impact Measurement

Problem

You want to retrieve data from one or more sources/spreadsheets.

Solution

SELECT is the most commonly used statement in SQL. The SELECT Statement in SQL is used to retrieve or fetch data from a table in a database. We can fetch either the entire table/dataset or specific columns in the table/datasets.   To create a simple SQL SELECT Statement, you must specify the column(s) name and the table name (dataset name).  SELECT Statement in SQL is referred as the entire query.

Syntax of SQL SELECT Statement:

SELECT column1, column2, column3, .... FROM table-name
[WHERE Clause]
[GROUP BY clause]
[HAVING clause]
[ORDER BY clause]

  • table-name is the name of the table from which the information is retrieved.
  • column1, column2,column3,....  includes one or more columns from which data is retrieved.
  • The code within the brackets [..] is optional. 

NOTE : There are two required components in any SQL query: SELECT and FROM—and they have to be in that order. SELECT indicates which columns we'd like to view, and FROM identifies the table that they reside. However, it is not case-sensitive.

Let's learn to use SELECT statement with an example :  

Consider the table "courses" that contains columns : id , course name, No of lessons. 

There are 6 records in this dataset and the information in this dataset. It can be accessed here

Course Name ID No of lessons
SQL Basics 1 12
SQL Pro 2 15
Laravel 3 10
Python 4 7
Excel Basics 5 5
Excel Pro 6 9

i) To fetch only one specific column from the data. For e.g. Fetch Course Name from "courses" table/dataset then the SELECT statement would be like:

SELECT "Course Name" from courses

TIP : You need to specify the column name to fetch the specific data.  In your use case, replace "Course Name" with  column name and the courses with data set name / table name according to your dataset

This would produce the following results : The result set will have only names.

Course Name
SQL Basics
SQL Pro
Laravel
Python
Excel Basics
Excel Pro

ii) To fetch specific columns from the dataset.

For eg. to fetch only "Course Name" and "No of lessons" from "courses" table / dataset then the SELECT statement would be like:

SELECT "Course Name","No of lessons" from courses

TIP : replace names of the columns separated by commas (,) and the table name according to your dataset and follow the syntax. Be mindful of double quotes("") and commas(,)

This would produce the following results : The result set will only have "Course Name" and "No of lessons" as specifc column names are specified in the SELECT statement.

Course Name No of lessons
SQL Basics 12
SQL Pro 15
Laravel 10
Python 7
Excel Basics 5
Excel Pro 9

 

iii) To fetch all the columns from the dataset/ table "courses".

The SELECT statement should be like this :

SELECT "Course Name", "ID", "No of lessons" from courses

OR

SELECT * from courses

NOTE :

* (asterisk)  Indicates "everything, all columns / fields" from the dataset. 

You can list all the columns separated by comma (,) or use *(asterisk) to list all the columns/fields in the dataset

However, The result set is the same in both the scenario as shown below :

Course Name ID No of lessons
SQL Basics 1 12
SQL Pro 2 15
Laravel 3 10
Python 4 7
Excel Basics 5 5
Excel Pro 6 9


IMPORTANT NOTE :

For instance, the columns labelled "No of lessons" and "Course Name" have spaces in column names. When used in a SELECT statement, these columns must be enclosed in double quotes like this: "No of lessons," "Course Name." Please take a close look at the SELECT statement written above. For table names and dataset names, a similar criterion applies.

Let's learn to use SELECT statement with Impact Cloud Dashboard

Login to Impact Cloud instance using the credentials. For this example, we will be using Impact Cloud Demo instance. 

Go to SQL Lab, Select SQL Editor.  

SQL Editor opens up as shown below.  

In the screen shot shown above

SQL Editor is a tool to write / execute queries on Impact Cloud

The area where the query/ SELECT statement is written is indicated by 1.
The area where the result set is displayed is indicated by 2.

REMEMBER :

1. While using the Impact Cloud, the database and schema where the dataset resides needs to be identified correctly. It is seen on the left hand side of the screen and can be selected using the drop down. 

2. Choosing the correct Database and Schema is mandatory before the query is executed.

 

i) To fetch only the Course Name from "courses" table/dataset then write the SELECT statement which we learnt above to the area indicated by 1 and click RUN to execute the query. 

SELECT "Course Name" from gc_demo.courses

The result is displayed as shown below which fetches only "Course Name" from courses dataset.

NOTE: Impact Cloud's dataset is identified by its schema name. dataset, therefore gc_demo.courses is used instead of courses.   gc_demo is the schema where the courses dataset resides.

So, when you adapt it to your context, replace the correct schema name and dataset according to your use case.

ii) Similarly, to fetch all the columns from the dataset/ table "courses" then write the SELECT statement learnt above to the specific area as explained above. Click on RUN to execute the query.

SELECT * from gc_demo.courses

The result is displayed as shown above which fetches all columns from courses dataset.

TO DO :

1. Write a SELECT Statement to fetch "Course Name" and "ID" from courses dataset / table.

2. Adapt the SELECT Statement to suit your dataset.

Congratulations !! You have learnt to use SELECT Statement.