Learning SQL in Practice: LeetCode Challenges and Setting Up PostgreSQL
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 valuesNOT NULL– disallows empty valuesPRIMARY 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,serialuuiddate,time,timestampvarchar(n),textjson,xmlbooleanmoney
This flexibility is useful for modeling real‑world data.
My First Table: users
To put everything together, I created a simple users table:
id→bigserial,PRIMARY KEY,NOT NULLfirst_name→varchar(50),NOT NULLlast_name→varchar(50)gender→varchar(6)email→varchar(150)dob→date
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.