A lightweight SQL-like query engine built in Python. This project executes a small subset of SQL queries on CSV-backed tables and demonstrates core database engine concepts such as query parsing, filtering, sorting, projection, and hash-based indexing.
- Load table data from CSV files
- Parse simple SQL-like queries
- Support
SELECTcolumn projection - Support
WHEREfiltering with comparison operators - Support
ORDER BYsorting - Use hash-based indexing for equality lookups
- Include unit tests with
pytest
SELECT columns FROM table;
SELECT columns FROM table WHERE column operator value;
SELECT columns FROM table WHERE column operator value ORDER BY column ASC;
SELECT columns FROM table WHERE column operator value ORDER BY column DESC;=, !=, >, <, >=, <=
The project includes a sample CSV file:
id,name,age,city,gpa
1,Alice,22,Phoenix,3.8
2,Bob,19,Dallas,3.2
3,Charlie,24,Phoenix,3.6
4,David,21,Seattle,3.4
5,Eva,23,Dallas,3.9
6,Frank,20,Chicago,3.1
7,Grace,25,Phoenix,3.7SELECT * FROM students;SELECT name, age FROM students WHERE age > 20;SELECT name, city FROM students WHERE city = 'Phoenix';SELECT name, gpa FROM students WHERE gpa >= 3.5 ORDER BY gpa DESC;Query:
SELECT name, gpa FROM students WHERE gpa >= 3.5 ORDER BY gpa DESC;Output:
{'name': 'Eva', 'gpa': '3.9'}
{'name': 'Alice', 'gpa': '3.8'}
{'name': 'Grace', 'gpa': '3.7'}
{'name': 'Charlie', 'gpa': '3.6'}
4 row(s) returned.
mini-sql-query-engine/
│
├── data/
│ └── students.csv
│
├── src/
│ ├── storage.py
│ ├── parser.py
│ ├── executor.py
│ ├── index.py
│ └── engine.py
│
├── tests/
│ ├── test_parser.py
│ ├── test_executor.py
│ └── test_index.py
│
├── main.py
├── requirements.txt
└── README.md
The query engine follows a simple execution pipeline:
SQL Query
↓
Parser
↓
Structured Query Dictionary
↓
CSV Table Loader
↓
WHERE Filtering or Indexed Lookup
↓
ORDER BY Sorting
↓
Column Projection
↓
Result Rows
Loads CSV files and converts each row into a Python dictionary.
Converts SQL-like query strings into structured Python dictionaries containing selected columns, table name, filter conditions, and sorting instructions.
Example parsed query:
{
"columns": ["name", "gpa"],
"table": "students",
"where": {
"column": "gpa",
"operator": ">=",
"value": "3.5"
},
"order_by": {
"column": "gpa",
"direction": "DESC"
}
}Applies filtering, sorting, and column selection on the loaded table rows.
The executor handles:
- Filtering rows using
WHERE - Sorting rows using
ORDER BY - Returning only selected columns using
SELECT
Builds a hash-based index on a selected column to optimize equality-based lookups.
Example index:
{
"Phoenix": [
{"id": "1", "name": "Alice", "age": "22", "city": "Phoenix", "gpa": "3.8"},
{"id": "3", "name": "Charlie", "age": "24", "city": "Phoenix", "gpa": "3.6"},
{"id": "7", "name": "Grace", "age": "25", "city": "Phoenix", "gpa": "3.7"}
],
"Dallas": [
{"id": "2", "name": "Bob", "age": "19", "city": "Dallas", "gpa": "3.2"},
{"id": "5", "name": "Eva", "age": "23", "city": "Dallas", "gpa": "3.9"}
]
}Hash indexing is used for equality-based queries such as:
SELECT name, city FROM students WHERE city = 'Phoenix';Instead of scanning every row, the engine can directly retrieve matching rows from the index.
Combines all components into a complete query execution flow.
The engine:
- Parses the SQL-like query
- Loads the target CSV table
- Uses an index when possible
- Applies filtering when needed
- Sorts the result
- Selects the requested columns
- Returns the final output
Clone the repository and install dependencies:
pip install -r requirements.txtRun the command-line interface:
python main.pyThen enter a query:
SELECT name, age FROM students WHERE age > 20;To exit the program:
exit
Run:
pytestExpected result:
10 passed
The test suite validates:
- Parsing
SELECT * - Parsing selected columns
- Parsing
WHEREconditions - Parsing
ORDER BY DESC - Filtering numeric values
- Filtering string values
- Sorting rows
- Selecting specific columns
- Building hash indexes
- Handling missing index values
This project intentionally supports only a small subset of SQL.
Currently not supported:
JOINGROUP BYHAVINGAND/ORconditions- Subqueries
- Aggregate functions
- SQL dialect support
- Persistent database storage
- Insert, update, or delete operations
- Add support for
ANDandORconditions - Add aggregate functions such as
COUNT,AVG, andSUM - Add
GROUP BY - Support multiple tables and basic joins
- Improve error handling for invalid tables and columns
- Add benchmarking for indexed vs non-indexed queries
- Add support for saving query results to a file
- Add a simple web interface using Flask or FastAPI