Session 4: Working with Data 

Main Topics Covered:

  1. Logical Operators
    • AND: Both conditions must be true.
    • OR: At least one condition must be true.
    • NOT: Reverses the condition (true → false, false → true).
  2. Filtering Data (WHERE Clause)
    • Used to select specific rows based on conditions.
    • Can use comparison operators (=, !=, >, <, >=, <=).
    • Can combine conditions using logical operators.
  3. Sorting Data (ORDER BY Clause)
    • Used to arrange data in ascending (ASC) or descending (DESC) order.
    • Can sort by multiple columns (primary column first, then secondary column).
  4. Limiting Data (LIMIT Clause)
    • Restricts the number of rows returned.
    • Commonly used with ORDER BY to get top results.

SQL Examples from the Session

1. Using Logical Operators

SELECT * FROM students 

WHERE age >= 18 AND name LIKE ‘J%’;

  • Finds students who are 18 or older and have names starting with J.

SELECT * FROM training_exercises 

WHERE difficulty_level = ‘hard’ AND is_completed = false;

  • Finds training exercises that are hard and not completed.

2. Sorting Data

SELECT * FROM students 

ORDER BY name ASC;

  • Sorts students alphabetically (A-Z).

SELECT * FROM students 

ORDER BY grade DESC, last_name ASC;

  • Sorts students by highest grade first, and if grades are equal, sorts by last name (A-Z).

3. Limiting Results

SELECT * FROM students 

ORDER BY grade DESC 

LIMIT 5;

  • Returns the top 5 students with the highest grades.

Additional Notes

  • DBBeaver Tips:
    • Right Click → Create Database/Table as a shortcut.
    • Use double hyphens () for comments in SQL.
    • The “Execute All” button runs the entire script.

Common Questions Answered

Can I use more than one database?
Yes, but you must specify which one using:


USE database_name;

  • Can I duplicate a database?
    Generally for backups or testing, but not common in practice.
  • Can I connect with others on LinkedIn?
    Not for this course (due to participants under 18 and safeguarding policies).
  • How do I know if my DBBeaver is connected?
    Look for the green checkmark next to your local host.
+

Leave a comment