How to Use SELECT in SQL? - Example and Implementation

SQL SELECT Made Easy: This recipe helps you with practical examples and hands-on implementation for seamless database querying. | ProjectPro

Recipe Objective - How to Use SELECT in SQL? - Example and Implementation  

Structured Query Language (SQL) is the foundation of database management and plays a pivotal role in extracting, manipulating, and managing data. The SELECT statement is arguably the most frequently used SQL command, allowing you to retrieve data from a database. Read this recipe to explore how to use SELECT in SQL, providing detailed examples and practical implementations to help you harness the power of SQL for data retrieval and analysis.

How to Use SELECT Query in SQL? 

The SELECT statement is one of the most frequently used SQL queries. It is used to retrieve data from one or more tables. The data is displayed in a table which is known as the result set.

Basic Syntax:

SELECT column_name1, column_name2,...

FROM table_name;

SELECT  * in SQL 

The Syntax to select all the data from a table is as follows:

SELECT  * 

FROM table_name;

Let us select all the fields from the customers table that has columns employee_id, employee_name, age, city, and country. 

Code:

SELECT * FROM customers;

Use of SELECT in SQL to extract all records

Output:

 +-------------+----------------+------+------------+---------+

| employee_id | employee_name  | age  | city       | country |

+-------------+----------------+------+------------+---------+

|         101 | Thomas Shelby  |   30 | Birmingham | England |

|         102 | Grace Burgess  |   28 | Dublin     | Ireland |

|         103 | Alfie Solomons |   40 | London     | England |

|         104 | Michael Gray   |   22 | New York   | USA     |

|         105 | May Carleton   |   29 | Sheffield  | England |

+-------------+----------------+------+------------+---------+

5 rows in set (0.00 sec)

If we just want to view the customer_id and customer_name columns from the table, we can do so using the following query –

Code:

SELECT employee_id, employee_name FROM customers;

Output:

+-------------+----------------+

| employee_id | employee_name  |

+-------------+----------------+

|         101 | Thomas Shelby  |

|         102 | Grace Burgess  |

|         103 | Alfie Solomons |

|         104 | Michael Gray   |

|         105 | May Carleton   |

+-------------+----------------+

5 rows in set (0.00 sec)

Level Up Your SQL Game: Explore real-world SQL Projects on ProjectPro to sharpen your data querying skills. 

DISTINCT Keyword in SQL 

The distinct keyword is used alongside the SELECT statement to retrieve distinct i.e. unique values of data.

Syntax:

SELECT DISTINCT column_name1, column_name2, ... 

FROM table_name;

Now, let us retrieve the distinct values from the income column of the customers table.

Code:

SELECT DISTINCT income 

FROM customers;

SQL query using DISTINCT Keyword

Output:

+----------+

| income   |

+----------+

| 90000000 |

| 10000000 |

| 78000000 |

| 56000000 |

+----------+

4 rows in set (0.01 sec)

We can see that the value 78000000 that appeared twice in the actual table, has been displayed only once when the DISTINCT keyword is used.

GCP Data Ingestion with SQL using Google Cloud Dataflow

TOP Keyword in SQL 

The TOP keyword is used alongside the SELECT statement to retrieve the top n number or n percent of rows. It is similar to what head function does in other programming languages.

Syntax:

SELECT TOP number/percent column_name1, column_name2, ... 

FROM table_name;

Example 1: Let us retrieve the top 3 incomes from the customers table. 

Code:

SELECT TOP 3 income FROM customers;

Image on the SELECT query using TOP

Example 2: Let us retrieve 40 Percent of income data from customers tables.

Code:
SELECT TOP 40 PERCENT income from customers;

Image on how to use SELECT command in SQL using TOP

Master SQL Commands Through Hands-on Experience by ProjectPro! 

Mastering SQL commands is not just about understanding theory, but gaining practical experience through real-world projects. It's the hands-on application that truly solidifies your SQL skills. ProjectPro, with its extensive repository of over 270+ projects based on data science and big data, provides an excellent platform for honing your SQL expertise. Working on these projects will help you transform your theoretical knowledge into valuable, practical skills that are essential in the data-driven world of today. So,  don't miss the chance to master SQL through real-world applications – Subscribe to ProjectPro Repository today!

What Users are saying..

profile image

Anand Kumpatla

Sr Data Scientist @ Doubleslash Software Solutions Pvt Ltd
linkedin profile url

ProjectPro is a unique platform and helps many people in the industry to solve real-life problems with a step-by-step walkthrough of projects. A platform with some fantastic resources to gain... Read More

Relevant Projects

House Price Prediction Project using Machine Learning in Python
Use the Zillow Zestimate Dataset to build a machine learning model for house price prediction.

Isolation Forest Model and LOF for Anomaly Detection in Python
Credit Card Fraud Detection Project - Build an Isolation Forest Model and Local Outlier Factor (LOF) in Python to identify fraudulent credit card transactions.

Build Regression Models in Python for House Price Prediction
In this Machine Learning Regression project, you will build and evaluate various regression models in Python for house price prediction.

Build Regression (Linear,Ridge,Lasso) Models in NumPy Python
In this machine learning regression project, you will learn to build NumPy Regression Models (Linear Regression, Ridge Regression, Lasso Regression) from Scratch.

NLP Project for Beginners on Text Processing and Classification
This Project Explains the Basic Text Preprocessing and How to Build a Classification Model in Python

Azure Text Analytics for Medical Search Engine Deployment
Microsoft Azure Project - Use Azure text analytics cognitive service to deploy a machine learning model into Azure Databricks

Build an End-to-End AWS SageMaker Classification Model
MLOps on AWS SageMaker -Learn to Build an End-to-End Classification Model on SageMaker to predict a patient’s cause of death.

Build a Hybrid Recommender System in Python using LightFM
In this Recommender System project, you will build a hybrid recommender system in Python using LightFM .

Build a Multi Class Image Classification Model Python using CNN
This project explains How to build a Sequential Model that can perform Multi Class Image Classification in Python using CNN

Learn How to Build a Linear Regression Model in PyTorch
In this Machine Learning Project, you will learn how to build a simple linear regression model in PyTorch to predict the number of days subscribed.