Title: How to Retrieve Data from a Single Table
Source: Murach’s SQL Server 2022 for Developers
Instructor: Mike Murach & Associates, Inc.
Year: 2023
Code and run SELECT
statements using language elements from this chapter.
Distinguish between base table values and calculated values in SELECT
statements.
Describe the use of a column alias.
Explain order of precedence and the use of parentheses in arithmetic expressions.
Describe the DISTINCT
keyword and the TOP
clause.
Explain comparison operators, logical operators, and parentheses in WHERE
clauses.
Describe the use of IN
, BETWEEN
, and LIKE
operators in WHERE
clauses.
Explain the use of IS NULL
clause in WHERE
clauses.
Describe the use of column names, aliases, calculated values, and column numbers in ORDER BY
clauses.
Discuss the use of OFFSET
and FETCH
clauses in ORDER BY
clauses.
Syntax: SELECT select_list [FROM table_source] [WHERE search_condition] [ORDER BY order_by_list]
Query: SELECT * FROM Invoices;
Output: 114 total rows, displaying InvoiceID, VendorID, Invoice Number, Date, Total, Payment Total, Credit Total, TermsID.
Query: SELECT InvoiceNumber, InvoiceDate, InvoiceTotal FROM Invoices ORDER BY InvoiceTotal;
Query: SELECT InvoiceID, InvoiceTotal, CreditTotal + PaymentTotal AS TotalCredits FROM Invoices WHERE InvoiceID = 17;
Query: SELECT InvoiceNumber, InvoiceDate, InvoiceTotal FROM Invoices WHERE InvoiceDate BETWEEN '2023-01-01' AND '2023-03-31' ORDER BY InvoiceDate;
Query: SELECT InvoiceNumber, InvoiceDate, InvoiceTotal FROM Invoices WHERE InvoiceTotal > 50000;
Syntax: SELECT [ALL|DISTINCT] [TOP n [PERCENT] [WITH TIES]] column_specification ...
SELECT * FROM table_name;
SELECT VendorName, VendorCity, VendorState FROM Vendors;
Arithmetic Example: SELECT InvoiceNumber, InvoiceTotal - PaymentTotal - CreditTotal AS BalanceDue FROM Invoices;
String Example: SELECT VendorContactFName + ' ' + VendorContactLName AS FullName FROM Vendors;
Date Example: SELECT InvoiceNumber, InvoiceDate, GETDATE() AS CurrentDate FROM Invoices;
Use aliases for better readability.
Non-named calculated columns example: SELECT InvoiceNumber, InvoiceDate, InvoiceTotal, InvoiceTotal - PaymentTotal - CreditTotal FROM Invoices;
String concatenation example: SELECT VendorCity, VendorState, VendorCity + VendorState FROM Vendors;
Query: SELECT InvoiceDate, GETDATE() AS 'Today's Date', DATEDIFF(day, InvoiceDate, GETDATE()) AS Age FROM Invoices;
Use: To eliminate duplicate rows.
Example: SELECT DISTINCT VendorCity, VendorState FROM Vendors;
Query with LIMIT: SELECT TOP 5 VendorID, InvoiceTotal FROM Invoices ORDER BY InvoiceTotal DESC;
With PERCENT: SELECT TOP 5 PERCENT VendorID, InvoiceTotal FROM Invoices ORDER BY InvoiceTotal DESC;
With TIES: SELECT TOP 5 WITH TIES VendorID, InvoiceDate FROM Invoices ORDER BY InvoiceDate ASC;
Syntax: WHERE expression_1 operator expression_2
=
Equal, >
Greater than, <
Less than, <=
Less than or equal to, >=
Greater than or equal to, <>
Not equal.
AND: Combines multiple conditions that must all be true.
OR: Combines multiple conditions where any must be true.
NOT: Reverses the condition's truth value.
Vendors located in Iowa: WHERE VendorState = 'IA';
Invoices with a balance due: WHERE InvoiceTotal - PaymentTotal - CreditTotal > 0;
Filtering with IN clause, BETWEEN clause, and LIKE clause.
Importance of parentheses to control evaluation order in compound conditions.
Query example: WHERE (InvoiceDate > '01/01/2023' OR InvoiceTotal > 500) AND InvoiceTotal - PaymentTotal - CreditTotal > 0;
Syntax: WHERE expression IS [NOT] NULL;
Examples of queries retrieving rows with null values and non-null values.
Syntax: ORDER BY expression [ASC|DESC] [...]
Single column ascending: ORDER BY VendorName;
Descending order: ORDER BY VendorName DESC;
Multiple columns: ORDER BY VendorState, VendorCity, VendorName;
Query: ORDER BY order_by_list OFFSET offset_row_count ROWS FETCH NEXT fetch_row_count ROWS ONLY;
Use it to retrieve a specific range of rows.