Learning SQL in Practice: LeetCode Challenges and Setting Up PostgreSQL

Published: (January 7, 2026 at 03:42 AM EST)
2 min read
Source: Dev.to

Source: Dev.to

Practicing SQL with LeetCode (SQL 50 Challenge)

As a side quest today, I started the SQL 50 Challenge on LeetCode, focusing on the SELECT section. I was able to complete the following problems:

  • Recyclable and Low Fat Products
  • Find Customer Referee
  • Big Countries
  • Article Views I
  • Invalid Tweets

All of them were labeled easy, and while they were fun, the key takeaway was understanding what each question was actually asking.

I retried some questions multiple times, not because they were hard, but to clearly map:

  • the problem statement
  • the required SQL logic
  • the final query

This reinforced that SQL is less about memorizing syntax and more about translating questions into queries.

Installing PostgreSQL and Exploring the Tools

After practice, I installed PostgreSQL, which came with:

  • pgAdmin (GUI client)
  • psql / SQL Shell (terminal‑based client)

The tutorial I’m following focuses heavily on using the terminal, so I spent most of my time in the SQL shell.

Basic PostgreSQL Commands I Learned

Creating and Dropping Databases

CREATE DATABASE db_name;
DROP DATABASE db_name;

Dropping Tables

DROP TABLE table_name;

Creating Tables in PostgreSQL

CREATE TABLE table_name (
  column_name datatype constraint
);

Common Constraints

  • NULL – allows empty values
  • NOT NULL – disallows empty values
  • PRIMARY KEY – uniquely identifies each record

Useful psql Terminal Commands

  • \? → shows help
  • \c db_name → connects to a database
  • \l → lists all databases on the server
  • \d → lists all tables
  • \d table_name → shows a table’s schema (columns, data types, constraints, indexes)

These commands make navigating the database much easier without relying on a GUI.

Exploring PostgreSQL Data Types

PostgreSQL supports a wide variety of data types, including:

  • bigserial, serial
  • uuid
  • date, time, timestamp
  • varchar(n), text
  • json, xml
  • boolean
  • money

This flexibility is useful for modeling real‑world data.

My First Table: users

To put everything together, I created a simple users table:

  • idbigserial, PRIMARY KEY, NOT NULL
  • first_namevarchar(50), NOT NULL
  • last_namevarchar(50)
  • gendervarchar(6)
  • emailvarchar(150)
  • dobdate
CREATE TABLE users (
  id         bigserial PRIMARY KEY NOT NULL,
  first_name varchar(50) NOT NULL,
  last_name  varchar(50),
  gender     varchar(6),
  email      varchar(150),
  dob        date
);

Designing this table from scratch helped solidify how columns, data types, and constraints work together.

Final Thoughts

Today felt very practical. Solving SQL problems on LeetCode sharpened my query thinking, while setting up PostgreSQL and creating tables made everything feel real. I’m starting to see databases not just as storage, but as systems designed to answer questions reliably and safely.

Tomorrow, I’ll focus more on inserting data and querying it meaningfully.

Back to Blog

Related posts

Read more »

Databases in 2025: A Year in Review

Article URL: https://www.cs.cmu.edu/~pavlo/blog/2026/01/2025-databases-retrospective.html Comments URL: https://news.ycombinator.com/item?id=46496103 Points: 39...