| SELECT Statements | Operators | Wildcards (with LIKE) |
|---|---|---|
|
|
|
The SELECT statement is the most frequently used command in SQL Server. It allows you to retrieve data from one or more tables.
To retrieve every column from a table, use the asterisk (*) wildcard.
SELECT * FROM Employees;
For better performance and clarity, it is best practice to specify exactly which columns you need, separated by commas.
SELECT FirstName, LastName, Department FROM Employees;
If your table contains duplicate values and you only want to see unique entries, use the DISTINCT keyword immediately after SELECT.
SELECT DISTINCT Department FROM Employees;
The WHERE clause is used to extract only those records that fulfill a specified condition.
= : Equal to<> or != : Not equal to> : Greater than< : Less than>= : Greater than or equal to<= : Less than or equal toSELECT * FROM Products WHERE Price >= 100;
You can combine multiple conditions using logical operators.
AND : True if ALL conditions separated by AND are true.OR : True if ANY of the conditions separated by OR is true.NOT : Reverses the boolean value of a condition.-- Using AND SELECT * FROM Employees WHERE Department = 'IT' AND Salary > 60000; -- Using OR SELECT * FROM Customers WHERE City = 'New York' OR City = 'London'; -- Using NOT SELECT * FROM Employees WHERE NOT Department = 'HR';
IN : Allows you to specify multiple possible values for a column (a cleaner alternative to multiple ORs).BETWEEN : Selects values within a given range (inclusive of both end points).-- Using IN
SELECT * FROM Customers WHERE Country IN ('USA', 'Canada', 'UK');
-- Using BETWEEN
SELECT * FROM Orders WHERE OrderDate BETWEEN '2023-01-01' AND '2023-12-31';
The LIKE operator is used in a WHERE clause to search for a specified pattern in a column. SQL Server provides specific wildcards to help build these patterns.
% : Represents zero, one, or multiple characters._ : Represents exactly one single character.[] : Represents any single character within the brackets.[^] : Represents any single character NOT within the brackets.-- Starts with 'A' SELECT * FROM Employees WHERE FirstName LIKE 'A%'; -- Ends with 'son' SELECT * FROM Employees WHERE LastName LIKE '%son'; -- Contains 'tech' anywhere in the string SELECT * FROM Companies WHERE CompanyName LIKE '%tech%'; -- Exactly 4 characters, starting with 'J' and ending with 'hn' (e.g., John) SELECT * FROM Employees WHERE FirstName LIKE 'J_hn'; -- Starts with 'A', 'B', or 'C' SELECT * FROM Customers WHERE City LIKE '[ABC]%'; -- Starts with any letter EXCEPT 'A', 'B', or 'C' SELECT * FROM Customers WHERE City LIKE '[^ABC]%';
The ORDER BY keyword is used to sort the result-set in ascending (ASC) or descending (DESC) order. By default, it sorts in ascending order.
-- Sort descending by Salary SELECT * FROM Employees ORDER BY Salary DESC; -- Sort by Department alphabetically, then by Salary descending SELECT * FROM Employees ORDER BY Department ASC, Salary DESC;
In SQL Server, the TOP clause limits the number of rows returned in the result set. This is incredibly useful for large tables. Always use TOP with an ORDER BY clause to ensure consistent results!
-- Get the 5 highest paid employees SELECT TOP 5 * FROM Employees ORDER BY Salary DESC;
Instead of a fixed number, you can request a percentage of the total records.
-- Get the top 10% most expensive products SELECT TOP 10 PERCENT * FROM Products ORDER BY Price DESC;