Showing posts with label SSMS Tutorial. Show all posts
Showing posts with label SSMS Tutorial. Show all posts

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!


Creating Databases, Tables & Data Types in SQL Server

Part 3: Databases, Tables, and Data Types in SQL Server

Microsoft SQL Server Tutorial Series: Beginner to Expert


Welcome back to our SQL Server tutorial series! Now that you’ve installed SQL Server and SSMS, it’s time to start building your database.


In this tutorial, you’ll learn:

  • How to create a database in SQL Server
  • What tables are and how to create them
  • Understanding SQL Server data types
  • Running your first SQL statements
  • Best practices for database structure and naming

๐Ÿ’ป A Quick Look at the SSMS Interface

Before we dive into SQL code, open SQL Server Management Studio (SSMS) and do the following:

  1. Connect to your server using localhost or your instance name.
  2. In the Object Explorer panel on the left, you’ll see the server node with folders like Databases, Security, and Server Objects.
  3. Click New Query in the toolbar to open a query editor window where you will write SQL commands.

๐Ÿ’ก Tip: You can also create databases and tables using the SSMS UI by right-clicking DatabasesNew Database or right-clicking TablesNew Table. However, learning to write SQL code is essential for mastering SQL Server.


๐Ÿ—️ Creating Your First Database

Let's create a new database called SchoolDB. Use this SQL command:

CREATE DATABASE SchoolDB;
GO

After running this, refresh the Databases node in Object Explorer to see SchoolDB.


๐Ÿ“ What is a Table?

A table is where your data is stored. It consists of:

  • Columns (fields): define the type of data
  • Rows (records): the actual data entries

Think of a table like a spreadsheet: columns are headers, and each row is a record.


✍️ Creating a Table in SQL Server

Now, create a Students table inside SchoolDB:

USE SchoolDB;
GO

CREATE TABLE Students (
    StudentID INT PRIMARY KEY,
    FirstName VARCHAR(50),
    LastName VARCHAR(50),
    BirthDate DATE,
    IsActive BIT
);
GO

Explanation of Columns:

Column Data Type Description
StudentID INT Integer, used as Primary Key
FirstName VARCHAR(50) Variable-length text (up to 50 characters)
LastName VARCHAR(50) Variable-length text (up to 50 characters)
BirthDate DATE Date of birth
IsActive BIT Boolean (0 = false, 1 = true)

๐Ÿ—ƒ️ Common SQL Server Data Types

Data Type Description Example Values
INT Whole numbers 1, 50, 1000
VARCHAR(n) Variable length text 'John', 'Hello World'
DATE Date only '2023-01-01'
DATETIME Date and time '2023-01-01 12:30:00'
DECIMAL Decimal numbers 99.99, 1200.75
BIT Boolean (0 or 1) 0 = False, 1 = True

➕ Inserting Data into a Table

Insert a sample student into the Students table:

INSERT INTO Students (StudentID, FirstName, LastName, BirthDate, IsActive)
VALUES (1, 'Alice', 'Johnson', '2005-03-15', 1);
GO

You can insert more records by changing the values.


๐Ÿ”Ž Querying the Data

To view all data in the Students table:

SELECT * FROM Students;
GO

Running this query will display all records. Congratulations! ๐ŸŽ‰ You have created and queried your first table in SQL Server.


๐Ÿงพ Quick SQL Cheat Sheet


CREATE DATABASE [DatabaseName];
CREATE TABLE [TableName] (
    Column1 DataType PRIMARY KEY,
    Column2 DataType,
    ...
);
INSERT INTO [TableName] (Column1, Column2, ...)
VALUES (Value1, Value2, ...);
SELECT * FROM [TableName];
  

๐Ÿ’ก Did You Know?

SQL Server automatically prevents duplicate primary keys, so you can't insert the same StudentID twice!


๐ŸŒ Real-World Example: School Database

Imagine a school system where the Students table stores student info. You could also have related tables like Courses, Grades, and Attendance, all stored in the same database.

This helps organize data efficiently and is a stepping stone to learning relational databases.

๐Ÿ–ผ️ Simple Database and Table Diagram

+------------------+
|    SchoolDB      |  <-- Database
+------------------+
         |
         v
+------------------+
|    Students      |  <-- Table
+------------------+
| StudentID (INT)  |
| FirstName (VARCHAR) |
| LastName (VARCHAR) |
| BirthDate (DATE)  |
| IsActive (BIT)    |
+------------------+

๐Ÿ“‹ Best Practices for Beginners

Tip Recommendation
Naming Use PascalCase or snake_case (e.g., StudentID, first_name)
Primary Keys Always define a primary key for every table
Data Types Choose appropriate data types to save space and improve performance
Comments Use -- for inline comments in SQL to document your code
Consistency Maintain consistent naming, formatting, and structure

✅ Summary

In this tutorial, you learned how to:

  • Create a database in SQL Server
  • Understand and create tables with columns and data types
  • Insert and query data using basic SQL statements
  • Follow best practices for database design and naming


๐Ÿ”— What’s Next?

Check out the previous tutorials to refresh your knowledge:

Stay tuned for Part 4: Writing Basic Queries (SELECT, WHERE, ORDER BY) where we’ll learn how to extract and filter data.


Have questions or want to share your progress? Drop a comment below! ๐Ÿ™Œ


Featured Post

GROUP BY, HAVING, and Aggregations in SQL Server Explained

Part 9: GROUP BY, HAVING, and Aggregations in SQL Server Microsoft SQL Server Tutorial Series: Beginner to Expert Welcome to Part 9 of...

Popular Posts