Chapter 9 Application Flashcards
Sort by the car column.
Select * from NASCAR2 order by car;
Sort by the car column explicitly cast as an integer.
Select * from NASCAR2 order by cast(car as signed);
Sort by the car column implicitly cast as an integer.
Select * from NASCAR2 order by car + 0;
Sort by the car column after it has been padded with leading zeros.
Select LPAD(car, 2, ‘0’) as car, driver from NASCAR2 order by car;
Apply the LOCATE function to find the blank character in the driver string.
Select driver, LOCATE(‘ ‘, driver) as space from NASCAR2;
Use SUBSTRING() in combination with LOCATE() to parse driver into first and last names.
Select driver, substring(driver, 1, LOCATE(‘ ‘,driver) - 1) as FirstName, Substring(driver, Locate(‘ ‘, driver)+1) as LastName from NASCAR2;
Invoke SUBSTRING_INDEX() to parse driver into first and last names.
Select SUBSTRING_INDEX(driver,’ ‘, 1) as FirstName, SUBSTRING_INDEX(driver,’ ‘, -1) as LastName from NASCAR2;
Prepare a query that projects car, amount, and integer values for amount returned by ROUND(), TRUNCATE(), FLOOR(), and CEILING(). Show the result set for cars 2 and 8.
Select Round(amount,0) as Rounded_Amount, Truncate(amount,0) as Truncated_Amount, Floor(amount) as RoundedDown_Amount, Ceiling(amount) as RoundedUp_Amount from NASCAR2 where car=08 or car=02;
Query NASCAR to projects all columns for those rows with amount values BETWEEN four and five thousand (dollars).
select * from NASCAR2 where amount BETWEEN 4000 and 5000;
Code a query that invokes NOW(), SYSDATE(), and CURDATE(). Show the result set given that this query is issued during the exam 2 period.
select now(), sysdate(), curdate();
Given a date type named lastRaceDate is added to NASCAR, prepare a query that projects driver and the three numeric values for month, day and year, and separated by forward slashes (e.g. 02/28/2015). Use CONCAT(), MONTH(), DAYOFWEEK(), and YEAR() in the SELECT clause.
Select concat(driver,’ ‘,lastRaceDate), Month(lastRaceDate) as Month, DayofWeek(lastRaceDate) as Day, Year(lastRaceDate) as Year from NASCAR2;