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

Ameeruddin Mohammed

ETL (Abintio) developer at IBM
linkedin profile url

I come from a background in Marketing and Analytics and when I developed an interest in Machine Learning algorithms, I did multiple in-class courses from reputed institutions though I got good... Read More

Relevant Projects

Ensemble Machine Learning Project - All State Insurance Claims Severity Prediction
In this ensemble machine learning project, we will predict what kind of claims an insurance company will get. This is implemented in python using ensemble machine learning algorithms.

MLOps Project to Build Search Relevancy Algorithm with SBERT
In this MLOps SBERT project you will learn to build and deploy an accurate and scalable search algorithm on AWS using SBERT and ANNOY to enhance search relevancy in news articles.

End-to-End ML Model Monitoring using Airflow and Docker
In this MLOps Project, you will learn to build an end to end pipeline to monitor any changes in the predictive power of model or degradation of data.

Customer Market Basket Analysis using Apriori and Fpgrowth algorithms
In this data science project, you will learn how to perform market basket analysis with the application of Apriori and FP growth algorithms based on the concept of association rule learning.

Recommender System Machine Learning Project for Beginners-3
Content Based Recommender System Project - Building a Content-Based Product Recommender App with Streamlit

Classification Projects on Machine Learning for Beginners - 2
Learn to implement various ensemble techniques to predict license status for a given business.

Create Your First Chatbot with RASA NLU Model and Python
Learn the basic aspects of chatbot development and open source conversational AI RASA to create a simple AI powered chatbot on your own.

AWS Project to Build and Deploy LSTM Model with Sagemaker
In this AWS Sagemaker Project, you will learn to build a LSTM model on Sagemaker for sales forecasting while analyzing the impact of weather conditions on Sales.

Customer Churn Prediction Analysis using Ensemble Techniques
In this machine learning churn project, we implement a churn prediction model in python using ensemble techniques.

NLP Project for Multi Class Text Classification using BERT Model
In this NLP Project, you will learn how to build a multi-class text classification model using using the pre-trained BERT model.