SQL Server Samples
Explore a collection of practical SQL Server code samples and examples to help you build powerful database applications.
-
Basic SELECT Statements
Learn how to retrieve data from your SQL Server tables using fundamental SELECT statements with WHERE and ORDER BY clauses.
SELECT CustomerID, CompanyName, City FROM Customers WHERE Country = 'USA' ORDER BY City;
-
Working with JOINs
Understand how to combine data from multiple tables using various types of JOINs, including INNER, LEFT, and RIGHT joins.
SELECT o.OrderID, c.CompanyName FROM Orders AS o INNER JOIN Customers AS c ON o.CustomerID = c.CustomerID WHERE o.OrderDate >= '2023-01-01';
-
Introduction to Stored Procedures
Discover how to create and execute stored procedures for encapsulating SQL logic and improving performance.
CREATE PROCEDURE GetCustomerOrders (@CustomerID INT) AS BEGIN SELECT OrderID, OrderDate FROM Orders WHERE CustomerID = @CustomerID; END; EXEC GetCustomerOrders @CustomerID = 10;
-
Aggregate Functions
Utilize aggregate functions like COUNT, SUM, AVG, MIN, and MAX to perform calculations on your data.
SELECT COUNT(CustomerID) AS TotalCustomers, AVG(Freight) AS AverageFreight FROM Orders WHERE ShipCountry = 'Germany';
-
Data Manipulation (INSERT, UPDATE, DELETE)
Learn the essential commands for inserting new data, updating existing records, and removing data from your tables.
INSERT INTO Products (ProductName, Price) VALUES ('New Gadget', 99.99); UPDATE Products SET Price = Price * 1.10 WHERE CategoryID = 5; DELETE FROM Customers WHERE CustomerID = 50;
-
Working with Dates and Times
Explore functions for manipulating and querying date and time data effectively in SQL Server.
SELECT OrderID, OrderDate, DATEPART(year, OrderDate) AS OrderYear FROM Orders WHERE OrderDate BETWEEN '2023-01-01' AND '2023-12-31';