VOOZH about

URL: https://dev.to/lamkhac/episode-1-t-sql-basics-in-microsoft-fabric-warehouse-select-where-and-order-by-3m89

⇱ T-SQL on Microsoft Fabric - Episode 1: Mastering Data Retrieval with SELECT, WHERE, and ORDER BY - DEV Community


T-SQL on Microsoft Fabric - Episode 1: Mastering Data Retrieval with SELECT, WHERE, and ORDER BY

Learning Goals

In this lesson, you will learn how to:

  • Read data from tables using SELECT
  • Filter rows with WHERE
  • Sort query results with ORDER BY
  • Get familiar with standard T-SQL syntax
  • Practice directly in Microsoft Fabric Warehouse

1. Understanding Database and Schema

In Fabric Warehouse, objects are commonly organized like this:

Warehouse
|
|-- sales
| |-- Customers
| |-- Orders
|
|-- hr
| |-- Employees
|
|-- finance
 |-- Transactions

Schemas help you:

  • Group related tables
  • Manage permissions
  • Organize large systems more effectively

2. Create a Schema

Create a schema for the sales dataset:

CREATE SCHEMA sales;

πŸ‘

Check existing schemas:

SELECT *
FROM sys.schemas;

πŸ‘

3. Create Tables

Create the Customers table:

CREATE TABLE sales.Customers
(
 CustomerID INT,
 CustomerName VARCHAR(100),
 City VARCHAR(50),
 Country VARCHAR(50)
);

Create the Orders table:

CREATE TABLE sales.Orders
(
 OrderID INT,
 CustomerID INT,
 OrderDate DATE,
 Amount DECIMAL(10,2)
);

πŸ‘

4. Insert Sample Data

Customers

INSERT INTO sales.Customers
VALUES
(1, 'John Smith', 'New York', 'USA'),
(2, 'Emma Brown', 'Chicago', 'USA'),
(3, 'David Wilson', 'London', 'UK'),
(4, 'Sophia Taylor', 'Manchester', 'UK'),
(5, 'Michael Lee', 'Singapore', 'Singapore');

Orders

INSERT INTO sales.Orders
VALUES
(101, 1, '2026-01-10', 1200.00),
(102, 1, '2026-01-15', 800.00),
(103, 2, '2026-01-20', 2500.00),
(104, 3, '2026-02-01', 500.00),
(105, 5, '2026-02-05', 3200.00);

πŸ‘

5. SELECT

Get all columns:

SELECT *
FROM sales.Customers;

πŸ‘

Get specific columns:

SELECT CustomerName,
 Country
FROM sales.Customers;

πŸ‘

6. Alias

Rename columns in the output:

SELECT CustomerName AS Customer,
 Country AS Nation
FROM sales.Customers;

πŸ‘

7. WHERE

Filter rows using conditions.

Customers in the USA:

SELECT *
FROM sales.Customers
WHERE Country = 'USA';

πŸ‘

Orders greater than 1000:

SELECT *
FROM sales.Orders
WHERE Amount > 1000;

πŸ‘

Orders between 500 and 2000:

SELECT *
FROM sales.Orders
WHERE Amount BETWEEN 500 AND 2000;

πŸ‘

Multiple conditions:

SELECT *
FROM sales.Customers
WHERE Country = 'UK'
 AND City = 'London';

πŸ‘

Use IN:

SELECT *
FROM sales.Customers
WHERE Country IN ('USA', 'UK');

πŸ‘

8. DISTINCT

Return unique values:

SELECT DISTINCT Country
FROM sales.Customers;

πŸ‘

9. ORDER BY

Sort query results.

Ascending order:

SELECT *
FROM sales.Orders
ORDER BY Amount;

πŸ‘

Descending order:

SELECT *
FROM sales.Orders
ORDER BY Amount DESC;

πŸ‘

Sort by multiple columns:

SELECT *
FROM sales.Customers
ORDER BY Country,
 CustomerName;

πŸ‘

10. TOP

Return the first N rows.

Top 3 orders:

SELECT TOP 3 *
FROM sales.Orders;

πŸ‘

Top 3 highest-value orders:

SELECT TOP 3 *
FROM sales.Orders
ORDER BY Amount DESC;

πŸ‘

11. Combining Clauses in a Real Query

Find the top 2 highest orders from customers in the USA:

SELECT TOP 2
 c.CustomerName,
 o.Amount
FROM sales.Customers c
JOIN sales.Orders o
 ON c.CustomerID = o.CustomerID
WHERE c.Country = 'USA'
ORDER BY o.Amount DESC;

πŸ‘


References