Welcome to Section 3. In Section 2, you learned how databases are designed and protected:
tables, keys, constraints, relationships, transactions, normalization, and indexes.
Now we move into the daily language of DBAs: T-SQL. If you want to be job-ready, you must be able to:
read queries quickly, write correct queries safely, and understand what your query is asking SQL Server to do.
This lesson focuses on the most important statement in SQL: SELECT.
You will learn the fundamentals in a way that matches real DBA work:
projection (what columns you return),
filtering (which rows you return),
and sorting (how results are ordered).
Lesson description: You will learn the SELECT statement structure, how SQL Server processes it conceptually,
how to choose the right columns, how to filter safely using WHERE, how sorting works with ORDER BY,
and how to avoid common beginner mistakes that cause incorrect results and performance problems.
You will also practice with DBA-style examples (auditing, troubleshooting, validation, reporting).
A SELECT query answers a question about your data. As a DBA, you use SELECT for:
Professional standard: Your SELECT queries must be correct, readable, and safe.
Wrong queries lead to wrong decisions.
At its simplest, SELECT has this shape:
SELECT column_list FROM table_name WHERE filter_condition ORDER BY sort_columns;
You will not always use every clause, but this pattern is the foundation.
Projection means selecting which columns to return.
New learners often use SELECT *. DBAs avoid this in most real work.
DBA best practice: explicitly list columns except for quick exploration in a safe environment.
SELECT
CustomerID,
FullName,
Email,
CreatedAt
FROM dbo.Customer;
Aliases improve readability for reports and troubleshooting.
SELECT
CustomerID AS CustomerId,
FullName AS CustomerName,
Email AS EmailAddress,
CreatedAt AS CreatedDate
FROM dbo.Customer;
DBA habit: write queries that another DBA can understand in 30 seconds.
The FROM clause defines the source table (or view). DBAs always qualify objects clearly.
In SQL Server, prefer dbo.TableName (or the correct schema) to avoid ambiguity.
SELECT BookingID, ConfirmationCode, PickupAt FROM dbo.Booking;
DBA note: In production, multiple schemas may exist (dbo, sales, security, etc.).
Schema-qualified queries are safer and clearer.
Filtering means selecting which rows to return based on conditions.
This is where correctness matters most.
SELECT
BookingID,
ConfirmationCode,
Status,
PickupAt
FROM dbo.Booking
WHERE Status = 'CONFIRMED';
Use parentheses with OR conditions. Without parentheses, you can return the wrong rows.
-- Confirmed bookings scheduled in the next 24 hours
SELECT
BookingID,
ConfirmationCode,
Status,
PickupAt
FROM dbo.Booking
WHERE Status = 'CONFIRMED'
AND PickupAt >= SYSDATETIME()
AND PickupAt < DATEADD(HOUR, 24, SYSDATETIME());
SELECT
BookingID,
ConfirmationCode,
Status
FROM dbo.Booking
WHERE Status IN ('NEW','CONFIRMED');
LIKE is useful, but remember that leading wildcards often prevent efficient index seeks.
-- Customers with emails in a specific domain SELECT CustomerID, FullName, Email FROM dbo.Customer WHERE Email LIKE '%@example.com';
NULL is not equal to anything, not even another NULL. You must use IS NULL or IS NOT NULL.
-- Find customers missing phone numbers SELECT CustomerID, FullName, Email FROM dbo.Customer WHERE Phone IS NULL;
DBA warning: Writing WHERE Phone = NULL returns no rows.
This is one of the most common beginner mistakes.
SQL tables have no guaranteed “natural order.” If you want results in a specific order, you must use ORDER BY.
SELECT BookingID, ConfirmationCode, PickupAt FROM dbo.Booking ORDER BY PickupAt;
SELECT BookingID, ConfirmationCode, PickupAt FROM dbo.Booking ORDER BY PickupAt DESC;
This is common in DBA reporting queries.
SELECT BookingID, ConfirmationCode, Status, PickupAt FROM dbo.Booking ORDER BY Status ASC, PickupAt DESC;
DBA performance note: ORDER BY can be expensive for large result sets because SQL Server may need to sort.
Good indexing can help when the ORDER BY matches the index key order.
SQL is written in one order, but it is logically processed in another. This matters when you write correct queries.
You will expand this later with GROUP BY and HAVING, but for now, memorize this sequence.
It prevents many confusing mistakes.
Use your lab tables (Customer, Booking, Service, Payment) and write these queries:
DBA habit: After each query, ask: “Did I return only what I need? Are the results correct? Is it readable?”
In the next lesson, we will go deeper into filtering and result control:
TOP for limiting output, DISTINCT for de-duplication, correct NULL handling patterns,
and professional “safe query” techniques DBAs use to avoid mistakes in production.
Not a member yet? Register now
Are you a member? Login now