PB

3.1

SQL Conditional Statements

  • IN clause

    • Used to determine if a value matches one of several values.

    • Example: WHERE column IN (value1, value2, value3).

  • BETWEEN clause

    • An alternative way to determine if a value lies between two other values.

    • Syntax: value BETWEEN minvalue AND maxvalue inclusive.

SQL Pattern Matching

  • LIKE operator

    • Characterizes any number of characters.

    • Example: LIKE 'cat%' matches any string that starts with "cat".

    • Matches one character: LIKE 'c_t' matches "cat", "cot", etc.

  • Case Sensitivity

    • By default, LIKE performs case-insensitive pattern matching.

    • If followed by the BINARY keyword, it performs case-sensitive matching.

SQL Queries: Examples

  • Example Queries

    • SELECT * FROM CountryLanguage WHERE CountryCode LIKE 'A_W';

      • This retrieves entries where CountryCode matches the pattern.

    • SELECT * FROM CountryLanguage WHERE Language LIKE 'A%n';

      • This retrieves languages starting with "A".

    • SELECT DISTINCT Language FROM CountryLanguage WHERE ISOfficial = 'F';

      • This returns unique languages where ISOfficial is False.

DISTINCT Keyword

  • DISTINCT clause

    • Used with SELECT to return only unique or distinct values.

    • Example in usage: SELECT DISTINCT Language FROM CountryLanguage;

    • This command retrieves unique languages from the CountryLanguage table.

SQL Ordering

  • ORDER BY clause

    • Utilized to sort rows in a specified order.

    • The DESC keyword indicates descending order.