SQL Server SELECT Query Tutorial – Retrieve Data Easily

Part 4: Your First SELECT Query

Microsoft SQL Server Tutorial Series: Beginner to Expert


Welcome to Part 4 of our SQL Server tutorial series! Now that you've created your first database and table, it’s time to learn how to retrieve data using the SELECT statement.


In this tutorial, you’ll learn how to:

  • Use the SELECT command to retrieve data
  • Select specific columns or all data from a table
  • Filter records with WHERE
  • Sort data using ORDER BY
  • Use TOP and DISTINCT keywords

๐Ÿ“ Using the SSMS Query Window

Open SQL Server Management Studio (SSMS):

  1. Connect to your local server
  2. Click New Query
  3. Select the SchoolDB database using the dropdown or write:
USE SchoolDB;
GO

๐Ÿ” SELECT * FROM Table

This is the simplest query to view all data from a table:

SELECT * FROM Students;
GO

๐Ÿ’ก Note: * selects all columns, but it's better to specify only what you need.


๐Ÿ“Œ Selecting Specific Columns

To view only first names and last names:

SELECT FirstName, LastName FROM Students;
GO

๐ŸŽฏ Filtering Records with WHERE

Use WHERE to filter specific rows. Example:

SELECT * FROM Students
WHERE IsActive = 1;
GO

This shows only students who are active.


๐Ÿ”ข Sorting Results with ORDER BY

To order students by their birth date:

SELECT * FROM Students
ORDER BY BirthDate ASC;
GO

Use DESC for descending order.


๐Ÿ” Using TOP to Limit Rows

To return just the first 3 rows:

SELECT TOP 3 * FROM Students;
GO

๐Ÿ†” Eliminating Duplicates with DISTINCT

If multiple students share the same first name:

SELECT DISTINCT FirstName FROM Students;
GO

๐Ÿงพ Quick SQL SELECT Cheat Sheet

-- Select all columns
SELECT * FROM TableName;

-- Select specific columns
SELECT Column1, Column2 FROM TableName;

-- Filter rows
SELECT * FROM TableName WHERE condition;

-- Sort rows
SELECT * FROM TableName ORDER BY Column ASC|DESC;

-- Return top N rows
SELECT TOP N * FROM TableName;

-- Remove duplicates
SELECT DISTINCT Column FROM TableName;
  

๐ŸŒ Real-World Example

Imagine you're a school administrator and want to find all active students born after 2010:

SELECT FirstName, LastName, BirthDate
FROM Students
WHERE IsActive = 1 AND BirthDate > '2010-01-01'
ORDER BY BirthDate;
GO

๐Ÿ–ผ️ Visual Result Example

+-----------+----------+------------+
| FirstName | LastName | BirthDate  |
+-----------+----------+------------+
| Alice     | Johnson  | 2012-03-15 |
| Brian     | Smith    | 2011-08-22 |
+-----------+----------+------------+

๐Ÿ“‹ Best Practices for SELECT Queries

Tip Recommendation
Use Column Names Avoid SELECT * for performance and clarity
Alias Columns Use AS to rename for readability: FirstName AS Name
Indent and Format Make code readable, especially in long queries
Use Comments Start comments with -- to describe your code


✅ Summary

In this lesson, you learned:

  • How to retrieve data using the SELECT statement
  • Filtering with WHERE and sorting with ORDER BY
  • Using TOP and DISTINCT for cleaner output
  • Best practices for writing readable SQL queries

๐Ÿ”— Navigation

Have a question? Drop it in the comments — we’d love to help!


No comments:

Post a Comment

Featured Post

Creating Your First MongoDB Database and Collection (Step-by-Step Tutorial)

Creating Your First MongoDB Database and Collection A Super Fun, Step-by-Step Adventure for Beginner to Expert Level What is MongoDB? ...

Popular Posts