Download presentation
Presentation is loading. Please wait.
Published byEzra Cooper Modified over 9 years ago
1
IST 220 – Intro to DB Lab 2 Specifying Criteria in SELECT Statements
2
We have learned in Lab 1 The SELECT statement can have up to six clauses The first two clauses SELECT and FROM are always used The ORDER BY clause, when used, will be the last one In this lab, we will practice the WHERE clause, which Comes right after the FROM clause, and Specifies criteria: only rows meet the conditions will be retrieved Basic SELECT Syntax
3
Clauses in a SELECT Statement SELECT … FROM … WHERE … … ORDER BY …
4
Many conditions can be specified using comparison operators (see list on right) These operators work with all 3 types of data commonly in use: Numbers Dates (e.g., >= means on or after a date) Text string (based on alphabetical order) Using Comparison Operators Comparison Operators These operators take 2 operands (or expressions) in the form Expr1 Operator Expr2 For instance, VenderState = ‘IA’ InvoiceTotal <> 0
5
We will use the Invoices table, and retrieve data from the invoice number, date, and total amount columns Q1: Retrieve invoices with a total amount greater than $1,000 Q2: Retrieve invoices with a date before Sept 1, 2009 Queries about Invoices
6
Sometimes a query needs to use a compound condition with multiple comparisons linked with logical operators: AND OR NOT Q3: Retrieve invoices with a total amount greater than $1,000 but less than $2,500 Q4: Retrieve invoices with a total amount less than $1,000 or greater than $2,500 Using Logical Operators
7
BETWEEN … AND … Specifying an inclusive range with continuous values Q5: Rewrite Q3 using this pattern (and notice the diff.) IN (…, … [, …]) Specifying a list of discrete values It can be to used to simplify compound conditions using OR (e.g., state = ‘CA’ or state = ‘DC’ or state = ‘IA’) Q6: Rewrite the compound condition above using the IN syntax Some Useful Operators
8
NULL is defined as a special value that stands for not available or not applicable For instance, an invoice that has not been paid will have a NULL value stored in the PaymentDate field The IS NULL phrase (and IS NOT NULL ) Null is not a specific value so you cannot use compare it using any of the comparison operators Q7: Find out invoice number, vendor ID, and invoice total of all invoices that have not been paid. Dealing with NULL Value
9
LIKE is used for matching strings A pattern is specified instead of exact values The following wildcard characters are often used: %matches any string of zero or more characters _ matches any one single character Q8: list name, city, and state of all vendors that are located in cities with a name starting with the letters 'san' The LIKE Keyword
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.