DEV Community

Cover image for Practicing SQL with Real Data: INSERT, WHERE, ORDER BY, and GROUP BY
Jessica Aki
Jessica Aki

Posted on

Practicing SQL with Real Data: INSERT, WHERE, ORDER BY, and GROUP BY

Day three was all about reinforcing the basics, not rushing forward, but getting comfortable with SQL by practicing it repeatedly on real data.

I spent most of today working through SQLZoo exercises, inserting data into PostgreSQL, and querying a realistic dataset generated with Mockaroo. This helped bridge the gap between theory and actual usage.

Inserting Data into Tables

I practiced inserting data into tables and also learned a very useful PostgreSQL feature: importing SQL files directly.

Using the psql terminal, I learned how to run SQL files with:

\i path_to_file/file_name.sql
Enter fullscreen mode Exit fullscreen mode

To test this properly, I downloaded a 1,000-row dataset from Mockaroo, adjusted some table parameters and constraints to match the data, and then imported it into my database.

Seeing a large dataset populate my tables instantly made everything feel much more real.

Querying Data with SELECT (Again, but Better)

I continued practicing the now very familiar SELECT statement, but with more structure and intention.

Sorting Results with ORDER BY

I learned how to sort query results using ORDER BY.

SELECT * FROM users ORDER BY first_name;

Enter fullscreen mode Exit fullscreen mode

By default, sorting is ascending (ASC), but it can be reversed using DESC.

SELECT * FROM users ORDER BY first_name DESC;
Enter fullscreen mode Exit fullscreen mode

Getting Unique Values with DISTINCT

To avoid duplicate results, I practiced using DISTINCT:

SELECT DISTINCT country FROM users;
Enter fullscreen mode Exit fullscreen mode

This was especially useful when working with location-based data.

Filtering with WHERE and Logical Operators

I practiced filtering results using WHERE, along with logical operators:

  • AND
  • OR

These allow conditions to be combined logically, making queries more precise.

Comparison Operators

I also learned and practiced comparison operators such as:

  • = (equal)
  • <, > (less than, greater than)
  • <=, >=
  • <> (not equal)

These were useful for filtering ages, dates, and numeric values in the dataset.

Limiting Results with LIMIT and OFFSET

I learned how to control how many rows are returned using:

SELECT * FROM users LIMIT 10 OFFSET 20;
Enter fullscreen mode Exit fullscreen mode

LIMIT controls how many rows are returned

OFFSET skips a specified number of rows

I also learned that LIMIT isn’t part of the SQL standard, which led me to discover the SQL-standard alternative.

Using FETCH Instead of LIMIT

PostgreSQL also supports the SQL-standard FETCH syntax:

SELECT * FROM users FETCH FIRST 6 ROWS ONLY;
Enter fullscreen mode Exit fullscreen mode

When I saw this for the first time, I was genuinely surprised and also very grateful that LIMIT exists.

Searching with IN and BETWEEN

I practiced filtering values using sets and ranges.

Using IN

SELECT * FROM users WHERE country IN ('Nigeria', 'Ghana', 'Kenya');
Enter fullscreen mode Exit fullscreen mode

Using BETWEEN

SELECT * FROM users WHERE dob BETWEEN '1990-01-01' AND '2000-12-31';
Enter fullscreen mode Exit fullscreen mode

These made queries much more expressive and readable.

Pattern Matching with LIKE and ILIKE

I revisited pattern matching using:

  • LIKE (case-sensitive)
  • ILIKE (case-insensitive)

I also learned about wildcards:

  • % → matches any number of characters
  • _ → matches exactly one character

This was especially useful for querying email patterns in the Mockaroo dataset.

Grouping Results with GROUP BY

Finally, I practiced grouping records using GROUP BY.

For example, I grouped users by their countries to see how many users belonged to each location:

SELECT country_of_birth, COUNT(country_of_birth) AS "Number of Users" FROM users GROUP BY country_of_birth ORDER BY COUNT(country_of_birth);
Enter fullscreen mode Exit fullscreen mode

This was my first real taste of how SQL starts to answer aggregate questions, not just retrieve rows.

Final Thoughts

Day three reinforced an important lesson: mastery comes from repetition.

Working with a real dataset made every query feel purposeful, and practicing the same concepts in different ways helped solidify my understanding. SQL is starting to feel less like syntax and more like a way of thinking.

Tomorrow, I’ll be learning and practicing even more.

Top comments (0)