All Projects → weinberg → SQLToy

weinberg / SQLToy

Licence: MIT license
An in-memory SQL database written in Javascript to demonstrate how SQL works.

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to SQLToy

learning-reverse-engineering
This repository contains sample programs written primarily in C and C++ for learning native code reverse engineering.
Stars: ✭ 83 (-20.19%)
Mutual labels:  educational-project
rich4clojure
Practice Clojure using Interactive Programming in your editor
Stars: ✭ 126 (+21.15%)
Mutual labels:  educational-project
Windows-DLL-Injector
Some DLL Injection techniques in C++ implemented for both x86 and x64 windows OS processes
Stars: ✭ 174 (+67.31%)
Mutual labels:  educational-project
stalin-sort
A repressive sorting algorithm (not really sorting)
Stars: ✭ 13 (-87.5%)
Mutual labels:  educational-project
AutomationShield
Arduino library and MATLAB/Simulink API for the AutomationShield Arduino expansion boards for control engineering education.
Stars: ✭ 22 (-78.85%)
Mutual labels:  educational-project
FA
Репозиторий практик факультета ИТиАБД направления Прикладной Информатики в Финансовом Университете при Правительстве РФ
Stars: ✭ 26 (-75%)
Mutual labels:  educational-project
notebooks-ci-showcase
Fully Configured Example of CI/CD For Notebooks On Top Of GCP
Stars: ✭ 22 (-78.85%)
Mutual labels:  educational-project
E-Learning-freesite
This site is mainly design for those student who don't know how to start their journey in the field of programming
Stars: ✭ 57 (-45.19%)
Mutual labels:  educational-project
zsh-launchpad
🚀 Simple, educational dotfiles template to get started with Zsh and learn about its features
Stars: ✭ 141 (+35.58%)
Mutual labels:  educational-project
tutorials
All of the code for my Medium articles
Stars: ✭ 130 (+25%)
Mutual labels:  educational-project
pico
This is a very simple HTTP server for Unix, using fork(). It's very easy to use.
Stars: ✭ 83 (-20.19%)
Mutual labels:  educational-project
jitsi-box
A Raspberry Pi based box to automate holding hybrid conferences with Jitsi
Stars: ✭ 15 (-85.58%)
Mutual labels:  educational-project
playlist-quarantine-edition
Playlist Quarantine Edition
Stars: ✭ 20 (-80.77%)
Mutual labels:  educational-project
savva3
Сайт Алексея Савватеева
Stars: ✭ 29 (-72.12%)
Mutual labels:  educational-project
PigletC
A toy C-like language compiler for PigletVM
Stars: ✭ 51 (-50.96%)
Mutual labels:  educational-project
Xacor
Experimental Game Engine
Stars: ✭ 24 (-76.92%)
Mutual labels:  educational-project
ESP For PubgMobile
🛩Pubg Mobile Hack using Extra Sensory Perception(ESP)🐱‍💻
Stars: ✭ 82 (-21.15%)
Mutual labels:  educational-project

What is SQLToy?

SQLToy is an in-memory SQL database written in Javascript. It is under 500 lines of code and has zero dependencies. It supports the following SQL operations:

- SELECT
- FROM
- CROSS JOIN
- INNER JOIN
- LEFT JOIN
- RIGHT JOIN
- WHERE
- GROUP BY
- Aggregate functions: ARRAY_AGG, MAX, MIN, COUNT
- DISTINCT
- ORDER BY
- OFFSET
- LIMIT
- CREATE TABLE
- INSERT INTO
- UPDATE

SQLToy is for learning SQL. Instead of doing this from the "outside-in" by learning the query language you do it "inside-out" by going through the implementation of the most common SQL operators. Instead of just learning what the operators do, you also learn how they do it which (hopefully!) gives you a much deeper level of understanding.

A detailed Wiki walks through the code. Please take a look!

To get the most out of this repository you should have familiarity with Javascript and some ES6 syntax. If you are proficient in another programming language like Python or Java you should still be able to figure things out. This is not a primer on how to write a production grade database. No attempts are made at performance optimization or compatibility with any standard. SQLToy does not support persistence, transactions or even parsing queries. There are many ways you can break it. See below for usage.

Read the Wiki

The heart of this repo is not the code itself but the walkthrough which describes what you need to understand it:

This can be found here: SQLToy Wiki

If you are a Javascript programmer I suspect you will find it much simpler than you think. Give it a look!

OK, I will read the Wiki. But how do I use this database?

There are two concepts you should understand in order to use the database. These are described in detail in the wiki section Key Concepts.

  1. SQL order of operations. You must call the operations in the correct order. A "real" database will do this for you. In this database you must call the operations in the correct order yourself.

  2. "Tables in, tables out". All operations in this database (and conceptually in a real database) take tables as input and produce tables as output. So you take a table and provide it to an operation, getting a new table back. To apply multiple operations, supply that new table as an argument to the next operation.

As an example, the following query in both PostgreSQL and SQLToy:

PostgreSQL
SELECT DISTINCT status, club.name, COUNT(*) AS count FROM employee
  JOIN employee_club 
    ON employee_club.a = employee.id
  JOIN club
    ON club.id = employee_club.b
  WHERE employee.salary > 80000
  GROUP BY status, club.name
  ORDER BY count DESC;

+----------+--------------------+---------+
| status   | name               | count   |
|----------+--------------------+---------|
| active   | Cat Lovers         | 2       |
| active   | Rotary Club        | 1       |
| active   | BBQ Crew           | 1       |
| active   | House Builders     | 1       |
| inactive | Education for Kids | 1       |
| inactive | Environmentalists  | 1       |
+----------+--------------------+---------+
SQLToy

To make this query in SQLToy we must re-order the operations according to the SQL Order of Operations and chain them together:

let employee = FROM('employee');
let employee_club = FROM('employee_club');
result = JOIN(employee, employee_club, (c) => c["employee_club.A"] === c["employee.id"]);
result = JOIN(result, club, (c) => c["employee_club.B"] === c["club.id"] );
result = WHERE(result, (row) => row['employee.salary'] > 150000);
result = GROUP_BY(result, ['employee.status', 'club.name']);
result = COUNT(result, 'club.name');
result = SELECT(result,['employee.status', 'club.name','COUNT(club.name)'],{'COUNT(club.name)': 'count'})
result = DISTINCT(result, ['employee.status', 'club.name', 'count'])
result = ORDER_BY(result, (a,b) => a.count < b.count ? 1 : -1);
table(result);

The resulting table can be viewed with the table() helper and looks like this:

┌─────────────────┬──────────────────────┬───────┐
│ employee.status │  club.name           │ count │
├─────────────────┼──────────────────────┼───────┤
│     active      │      Cat Lovers      │   2   │
│     active      │  Environmentalists   │   1   │
│     active      │  Food for the Needy  │   1   │
│     active      │    House Builders    │   1   │
│    inactive     │  Education for Kids  │   1   │
│    inactive     │  Environmentalists   │   1   │
└─────────────────┴──────────────────────┴───────┘

References

A good primer on SQL operation execution order:

https://blog.jooq.org/a-beginners-guide-to-the-true-order-of-sql-operations/

For a good discussion of set theory as it applies to the relational model:

https://medium.com/basecs/set-theory-the-method-to-database-madness-5ec4b4f05d79 .

Contributions

I welcome contributions! The goals of this project are:

  • Teach SQL through a simple Javascript SQL database implementation
  • Simplicity and readability over features or compatibility with any standard
  • As few dependencies as possible

If you have ideas to further these goals you are welcome to submit a pull-request.

License

The code in SQLToy is licensed under The MIT License.

The contents of the Wiki are licensed separately under the Creative Commons Attribution-NonCommercial-NoDerivatives 4.0 International License.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].