0% found this document useful (0 votes)
0 views7 pages

SQL Server Query Examples

The document outlines various SQL techniques including basic data retrieval, joins, aggregation, subqueries, window functions, ETL processes, and indexing. It provides examples of SQL queries for selecting data, counting records, joining tables, calculating sales, and modifying data. The document serves as a guide for performing common database operations and optimizing queries.

Uploaded by

Dharna Ahuja
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views7 pages

SQL Server Query Examples

The document outlines various SQL techniques including basic data retrieval, joins, aggregation, subqueries, window functions, ETL processes, and indexing. It provides examples of SQL queries for selecting data, counting records, joining tables, calculating sales, and modifying data. The document serves as a guide for performing common database operations and optimizing queries.

Uploaded by

Dharna Ahuja
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 7

1.

Basic Data Retrieval


• -- Select specific columns
• SELECT CustomerID, FirstName, LastName,
Email
• FROM Customers
• WHERE IsActive = 1
• ORDER BY LastName;

• -- Count total records


• SELECT COUNT(*) AS TotalOrders
2. Joins
• -- Inner Join: Customers with their Orders
• SELECT C.CustomerID, C.FirstName,
C.LastName, O.OrderID, O.OrderDate
• FROM dbo.Customers AS C
• INNER JOIN dbo.Orders AS O
• ON C.CustomerID = O.CustomerID
• WHERE O.OrderDate >= DATEADD(month, -1,
GETDATE());
3. Aggregation & Grouping
• -- Sales by product
• SELECT P.ProductName, SUM(OI.Quantity *
OI.UnitPrice) AS TotalSales
• FROM dbo.OrderItems AS OI
• JOIN dbo.Products AS P ON OI.ProductID =
P.ProductID
• GROUP BY P.ProductName
• ORDER BY TotalSales DESC;
4. Subqueries & CTEs
• -- Customers with above-average order count
• SELECT CustomerID, FirstName, LastName
• FROM dbo.Customers
• WHERE CustomerID IN (
• SELECT CustomerID
• FROM dbo.Orders
• GROUP BY CustomerID
• HAVING COUNT(*) > (
• SELECT AVG(order_count)
5. Window Functions
• -- Running total by date
• SELECT OrderDate,
• SUM(Quantity * UnitPrice) OVER (ORDER
BY OrderDate) AS RunningTotal
• FROM dbo.OrderItems
• JOIN dbo.Orders USING (OrderID);

• -- Percentage contribution by product


• SELECT ProductID, OrderDate,
6. ETL / Data Modification
• -- Insert into archive
• INSERT INTO dbo.Orders_Archive (OrderID,
CustomerID, OrderDate, Total)
• SELECT OrderID, CustomerID, OrderDate, Total
• FROM dbo.Orders
• WHERE OrderDate < DATEADD(year, -1,
GETDATE());

• -- Batch update
7. Indexing & Optimization Helpers
• -- Display missing indexes
• SELECT TOP 5
• migs.avg_user_impact AS Impact,
• mid.statement AS TableName,
• mid.equality_columns,
• mid.inequality_columns,
• mid.included_columns
• FROM sys.dm_db_missing_index_group_stats
AS migs

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy