SQL SELECT, WHERE, and ORDER BY for Beginners
SQL lets you retrieve and work with data stored in relational databases. Three statements and clauses are especially important for beginners: SELECT chooses the data, WHERE filters rows, and ORDER BY controls the result order.
Use a simple table
Imagine an Employees table containing columns such as Name, Department, and Salary. The examples below use that simple model.
SELECT
SELECT Name, Department
FROM Employees;
This returns the selected columns from the Employees table. You can select more columns when the query needs them.
SELECT all columns
SELECT *
FROM Employees;
The asterisk requests every column. It is convenient while exploring data, but explicit column names are often clearer for application queries because they document exactly what the query needs.
WHERE
SELECT Name, Department
FROM Employees
WHERE Department = 'IT';
WHERE filters rows so you only get records matching a condition. Comparisons can also use operators such as >, <, >=, and <>.
Combine conditions
SELECT Name, Salary
FROM Employees
WHERE Department = 'IT'
AND Salary >= 50000;
AND requires both conditions to be true. OR can be used when either condition is acceptable. Parentheses are useful when a query contains a mixture of AND and OR.
ORDER BY
SELECT Name, Salary
FROM Employees
ORDER BY Salary DESC;
DESC sorts from high to low, while ASC sorts from low to high. Ascending order is the default when no direction is specified.
Sort by more than one column
SELECT Department, Name, Salary
FROM Employees
ORDER BY Department ASC, Salary DESC;
The database first sorts by department. Within each department, it sorts salaries from highest to lowest.
NULL values
NULL represents the absence of a value, so it should not be compared with = or <>. Use IS NULL or IS NOT NULL when filtering missing values.
SELECT Name
FROM Employees
WHERE Department IS NULL;
Common beginner mistakes
- Forgetting to specify the table after
FROM. - Using text values without quotes.
- Expecting
WHERE column = NULLto find missing values. - Confusing the filtering role of
WHEREwith the sorting role ofORDER BY.
Learning path
Once these basics are comfortable, continue with the SQL Server Tutorials pillar for joins, CTEs, indexes, window functions, transactions, and query performance.
đŦ Comments
Sign in with Google to publish immediately, or comment anonymously and wait for approval.
Comments will appear here when available.