1 / 13

Lab_04: Basic SQL

Lab_04: Basic SQL. Outline. The ORDER BY Keyword SQL ORDER BY Syntax SQL NULL Values. The ORDER BY Keyword. The ORDER BY keyword is used to sort the result-set by a specified column. The ORDER BY keyword sort the records in ascending order by default.

austin-york
Download Presentation

Lab_04: Basic SQL

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Lab_04: Basic SQL

  2. Outline • The ORDER BY Keyword • SQL ORDER BY Syntax • SQL NULL Values

  3. The ORDER BY Keyword • The ORDER BY keyword is used to sort the result-set by a specified column. • The ORDER BY keyword sort the records in ascending order by default. • If you want to sort the records in a descending order, you can use the DESC keyword.

  4. SQL ORDER BY Syntax • SQL ORDER BY Syntax SELECT column_name(s) FROM table_name ORDER BY column_name(s) ASC|DESC

  5. ORDER BY Example The "Persons" table: Now we want to select all the persons from the table above, however, we want to sort the persons by their last name.

  6. ORDER BY Example • We use the following SELECT statement: SELECT * FROM Persons ORDER BY LastName The result-set will look like this:

  7. ORDER BY DESC Example The "Persons" table: • Now we want to select all the persons from the table above, however, we want to sort the persons descending by their last name.

  8. ORDER BY DESC Example We use the following SELECT statement: SELECT * FROM Persons ORDER BY LastName DESC The result-set will look like this:

  9. SQL NULL Values • NULL values represent missing unknown data. • By default, a table column can hold NULL values. • This chapter will explain the IS NULL and IS NOT NULL operators.

  10. SQL NULL Values • If a column in a table is optional, we can insert a new record or update an existing record without adding a value to this column. This means that the field will be saved with a NULL value. • NULL values are treated differently from other values. • NULL is used as a placeholder for unknown or inapplicable values. • Note: It is not possible to compare NULL and 0; they are not equivalent.

  11. SQL Working with NULL Values Look at the following "Persons" table: Suppose that the "Address" column in the "Persons" table is optional. This means that if we insert a record with no value for the "Address" column, the "Address" column will be saved with a NULL value. How can we test for NULL values? It is not possible to test for NULL values with comparison operators, such as =, <, or <>. We will have to use the IS NULL and IS NOT NULL operators instead.

  12. SQL IS NULL How do we select only the records with NULL values in the "Address" column? We will have to use the IS NULL operator: SELECT LastName,FirstName,Address FROM Persons WHERE Address IS NULL The result-set will look like this: Tip: Always use IS NULL to look for NULL values

  13. SQL IS NOT NULL How do we select only the records with no NULL values in the "Address" column? We will have to use the IS NOT NULL operator: SELECT LastName,FirstName,Address FROM PersonsWHERE Address IS NOT NULL The result-set will look like this:

More Related