170 likes | 272 Views
Selecting Rows from Tables. Now that we know how to specify columns, how do we specify rows in a SELECT statement?. What we know so far. SELECTING COLUMNS ORDERING ROWS SELECTING UNIQUE VALUES. SELECTING COLUMNS. SELECT * FROM table_name; Select ALL columns
E N D
Selecting Rows from Tables Now that we know how to specify columns, how do we specify rows in a SELECT statement?
What we know so far • SELECTING COLUMNS • ORDERING ROWS • SELECTING UNIQUE VALUES
SELECTING COLUMNS • SELECT * FROM table_name; • Select ALL columns • SELECT column_name [, column_name …] FROM table_name; • Select SOME columns
ORDERING ROWS • SELECT * FROM table_name ORDER BY column_name [DESC] [, column_name [DESC]…]; • SELECT column_name [, column_name …] FROM table_name ORDER BY column_number [DESC] [, column_number [DESC] …];
SELECTING UNIQUE VALUES • SELECT DISTINCT column_name FROM table_name • Select unique values from a column • SELECT COUNT(*) FROM table_name; • Count number of rows in a table.
ROWS • Now that we can determine which COLUMNS appear in a query, and the ORDER of the ROWS, how do we specify WHICH ROWS appear?
The ‘WHERE’ clause • The ‘WHERE’ clause appears after the specification of the table name: • SELECT column_specification FROM table_name where_clause [ORDER BY …]; • The WHERE clause looks at EACH ROW to determine if it will be output. • The specified condition must evaluate to TRUE in order for the ROW to appear. • Only the Columns specified for the Row are output.
How to define the WHERE condition • Compare columns to values: • WHERE column1 > 10 • WHERE Status = ‘VALID’ • WHERE NOT Name = ‘Smith’ • Compare columns to one another • WHERE column1 > column2 • WHERE maxSalary = currentSalary
For more complicated conditions, use OR and AND. • WHERE LastName = ‘Smith’ AND FirstName = ‘James’ • WHERE LastName = ‘Smith’ OR LastName = ‘Bernardo’
To USE more than one AND or OR: • Use Parentheses • WHERE FirstName = ‘James’ AND ( LastName = ‘Monroe’ OR LastName = ‘Madison’ )
Putting it all together • Decide on the columns (and order of columns) you will output. • SELECT column_specification FROM table_name WHERE where_condition ORDER BY order_specification; • Determine the condition for including rows. • SELECT column_specification FROM table_name WHERE where_condition ORDER BY order_specification; • Specify the order of the included rows. • SELECT column_specification FROM table_name WHERE where_condition ORDER BY order_specification;
Exercises • Use this table, which we will call “Electives”
Exercise 1 Select all records from the table where there are seats left. SELECT * FROM Electives WHERE __________;
Exercise 2 Select all records where there are seats left, and the room is either 516 or 522. SELECT * FROM Electives WHERE ________
Exercise 3 Select the room numbers and meeting times of courses that are not full (have seats left) SELECT _________ FROM Electives WHERE ___________________
Exercise 4 Select the coursenames that either ‘Dinallo’ teaches and order them by room number. SELECT coursename FROM Electives WHERE _______________ ORDER BY ______________
Exercise 5 • Select the courses and seats left that are not on the ‘third’ floor (e.g. room is not 300-399) ordered by number of seats left, with the most seats left listed first.