SQL and RethinkDB share very similar terminology. Below is a table of terms and concepts in the two systems.
SQL | RethinkDB |
---|---|
database | database |
table | table |
row | document |
column | field |
table joins | table joins |
primary key | primary key (by default id ) |
index | index |
This is a list of queries for inserting data into a database.
SQL | ReQL |
---|---|
INSERT INTO users(user_id, age, name) VALUES ("f62255a8259f", 30, Peter) | r.table("users").insert({ :user_id => "f62255a8259f", :age => 30, :name => "Peter" }) |
This is a list of queries for selecting data out of a database.
This is a list of commands for updating data in the database.
This is a list of queries for deleting data from the database.
SQL | ReQL |
---|---|
DELETE FROM users |
r.table("users").delete() |
DELETE FROM users WHERE age < 18 |
r.table("users").filter{ |doc| doc["age"] < 18 }.delete() |
This is a list of queries for performing joins between multiple tables.
SQL | ReQL |
---|---|
SELECT * FROM posts JOIN users ON posts.user_id = users.id |
r.table("posts").inner_join( r.table("users") ) { |post, user| post["user_id"] == user["id"] }.zip() Note: If you have an index (primary key or secondary index) built on the field of the right table, you can perform a more efficient join with eq_join. r.table("posts").eq_join("id", r.table("users"), :index => "id" ).zip() |
SELECT posts.id AS post_id, user.name, users.id AS user_id FROM posts JOIN users ON posts.user_id = users.id SELECT posts.id AS post_id, user.name, users.id AS user_id FROM posts INNER JOIN users ON posts.user_id = users.id |
r.table("posts").inner_join( r.table("users") ) { |post, user| post["user_id"] == user["id"] }.map { |post, user| :post_id => post["id"], :user_id => user["id"], :name => user["name"] } |
SELECT * FROM posts RIGHT JOIN users ON posts.user_id = users.id SELECT * FROM posts RIGHT OUTER JOIN users ON posts.user_id = users.id |
r.table("posts").outer_join( r.table("users") ) { |post, user| post["user_id"] == user["id"] }.zip() Note: You can perform more efficient r.table("posts").concat_map{ |post| r.table("users").get_all( post["id"], :index => "id" ).do{ |results| r.branch( results.count() == 0, [{:left => post}], results.map { |user| {:left => post, :right => user} } )} }.zip() |
SELECT * FROM posts LEFT JOIN users ON posts.user_id = users.id SELECT * FROM posts LEFT OUTER JOIN users ON posts.user_id = users.id |
r.table("posts").outer_join( r.table("users") ) { |user, post| post["user_id"] == user["id"] }.zip() r.table("posts").concat_map{ |post| r.table("users").get_all( post["id"], :index => "id" ).do{ |results| r.branch( results.count() == 0, [{:left => user}], results.map { |post| {:left => user, :right => post} } )} }.zip() |
This is a list of queries for performing data aggregation.
This is a list of queries for creating and dropping tables and databases.
Browse the following resources to learn more about ReQL:
© RethinkDB contributors
Licensed under the Creative Commons Attribution-ShareAlike 3.0 Unported License.
https://rethinkdb.com/docs/sql-to-reql/ruby/