LATEST 🚀 Welcome to Upgradix by Shrash Studio – Your destination for free technology learning and career growth.
🕘 Updated:
MS SQL Server

SQL SELECT Statement – Retrieving Data from Tables

The SELECT statement retrieves data from SQL Server tables. We can choose specific columns or retrieve every column. It is one of the most frequently used SQL statements in application development.

SQL SELECT Statement – Retrieving Data from Tables

Quick Answer: The SELECT statement retrieves data from SQL Server tables. We can choose specific columns or retrieve every column. It is one of the most frequently used SQL statements in application development.

1. Why Do We Need the SQL SELECT Statement?

Imagine Arjun works at a cinema ticket counter in Bengaluru. His cinema stores 10,000 ticket records every month.

Each ticket contains a movie name, customer name, seat number and ticket price. Arjun needs to find customer bookings quickly.

How can he retrieve the required information without reading every record manually?

The problem without the SELECT statement

Arjun could maintain a separate list inside his application. He could display ticket information from that list.

Example 1: Display cinema tickets using hardcoded application data

This C# example works, but it keeps ticket records inside application code.

var tickets = new[]
{
    "101, Pushpa 2, A1, Ravi",
    "102, Kalki 2898 AD, B2, Priya",
    "103, Pushpa 2, A3, Lakshmi"
};

foreach (var ticket in tickets)
{
    Console.WriteLine(ticket);
}

Expected output

101, Pushpa 2, A1, Ravi
102, Kalki 2898 AD, B2, Priya
103, Pushpa 2, A3, Lakshmi

This example displays the tickets correctly. However, the design has problems.

  1. Ticket records are stored directly inside application code.
  2. New bookings need changes to the application data.
  3. Different application instances cannot automatically share this list.
  4. Retrieving specific columns needs additional application logic.

Now imagine Arjun handling 10,000 bookings every month. Maintaining separate lists becomes difficult.

We need a database query that retrieves exactly the information required. We also need a simple way to select columns.

The solution: SQL SELECT Statement

SELECT = a SQL statement that retrieves data from database tables.

FROM = specifies the table from which SQL Server retrieves data.

Arjun can use one query to retrieve the required ticket information. SQL Server reads the requested data and returns the result.

Animation 1: How the SELECT statement retrieves data

Follow Arjun's request from the cinema counter to the returned ticket records.

Arjun Cinema ticket counter
requests ticket details
SELECT Query TicketId, MovieName, CustomerName
reads the requested table
SQL Server Processes the SELECT statement
retrieves requested columns
CinemaTickets Ticket records stored in SQL Server
returns matching data
Query Result 101 Pushpa 2 Ravi
102 Kalki 2898 AD Priya
Query request SQL processing Retrieved data Current animation stage

Arjun requests three columns. SQL Server retrieves those columns and returns the ticket records.

2. Implement the SELECT Statement in SQL Server

Let's use a cinema ticket database for our practical examples.

SELECT * = retrieves all columns from the selected table.

SELECT column_names = retrieves only the columns we specify.

Retrieve all columns using SELECT *

Example 2: Retrieve all cinema ticket records

Assume CinemaTickets already contains the following sample records.

Sample CinemaTickets table
TicketId MovieName SeatNumber CustomerName TicketPrice
101 Pushpa 2 A1 Ravi 250
102 Kalki 2898 AD B2 Priya 300
103 Pushpa 2 A3 Lakshmi 250
SELECT *
FROM CinemaTickets;

Expected output

TicketId  MovieName      SeatNumber  CustomerName  TicketPrice
101       Pushpa 2       A1          Ravi          250
102       Kalki 2898 AD  B2          Priya         300
103       Pushpa 2       A3          Lakshmi       250

The asterisk (*) requests every column. SQL Server returns all columns from CinemaTickets.

Retrieve specific columns

Sometimes Arjun only needs movie names and customer names. Retrieving every column is unnecessary for this request.

SELECT
    MovieName,
    CustomerName
FROM CinemaTickets;

Expected output

MovieName      CustomerName
Pushpa 2       Ravi
Kalki 2898 AD  Priya
Pushpa 2       Lakshmi

Notice that we listed only two column names. SQL Server returns only those requested columns.

Best practice: Prefer specific column names instead of SELECT * when you know which columns your application needs. This makes queries easier to understand and maintain.

3. Quick Summary

SELECT * vs SELECT specific columns

Comparing two ways to retrieve table data
SELECT * SELECT specific columns
Retrieves every column. Retrieves only requested columns.
Useful for quick inspection. Useful for application queries.
May return unnecessary data. Returns a focused result.
Column changes can affect returned results. Explicit columns make the result more predictable.

The important terms to remember

  • SELECT: Retrieves data from a database.
  • FROM: Specifies the source table or view.
  • Column: A named field inside a database table.
  • SELECT *: Retrieves all columns.
  • Result Set: The rows and columns returned by a query.

4. Common Mistakes and Best Practices

1. Using SELECT * in every query

It may retrieve columns that your application never uses.

Better approach: Specify the required columns explicitly.

2. Forgetting the FROM clause

SQL Server needs a source table for ordinary table retrieval queries.

Better approach: Specify the correct table after FROM.

3. Using incorrect column names

A misspelled column name can cause an invalid column error.

Better approach: Check the table structure before writing queries.

5. How SELECT Comes Up in Interviews

Question 1: What is the SELECT statement?

What the interviewer expects: Explain how data retrieval works.

Strong answer: "SELECT retrieves data from database tables. We can retrieve every column or specify only the columns our application needs."

Question 2: What is the difference between SELECT * and SELECT column names?

What the interviewer expects: Explain column selection.

Strong answer: "SELECT star retrieves every column. Selecting specific column names returns only the requested columns, making the result more focused."

Question 3: Does SELECT change the original table data?

What the interviewer expects: Understand read operations.

Strong answer: "No. SELECT retrieves data without changing the stored records. INSERT, UPDATE and DELETE are used for data modification."

6. Hands-On Practice

Beginner: School database

Retrieve StudentId and StudentName from a Students table. Also retrieve every column.

Expected result: Two queries with different column selections.

Intermediate: Hospital database

Retrieve PatientName, AppointmentDate and DoctorName from a HospitalAppointments table.

Hint: Specify the three required columns after SELECT.

Advanced: Banking database

Retrieve AccountNumber, CustomerName and AccountBalance from a BankAccounts table.

  • Write a query that retrieves every column.
  • Write another query that retrieves only two columns.
  • Compare both result sets.

Challenge: Explain why a production API should avoid unnecessary columns.

7. Next Steps and Community

Previous article: SQL Server Constraints – Primary Key, Foreign Key, Unique and Check

Next article: WHERE, ORDER BY, DISTINCT and TOP in SQL Server

For more details, read the official Microsoft documentation:

Practice retrieving different columns from your cinema database. Share your queries and discuss your results with other learners.

Comments & Ratings (0)

Log in or create a student account to comment and rate this article.

Be the first to comment on this article.