You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: core-concepts/modules/ROOT/pages/typeql/sql-vs-typeql.adoc
+28-28Lines changed: 28 additions & 28 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -19,7 +19,7 @@ Each `person` has a name, email, and age. Each `company` has a name. The `employ
19
19
20
20
Example SQL table definitions:
21
21
22
-
[sql]
22
+
[,sql]
23
23
----
24
24
CREATE TABLE person (
25
25
id SERIAL PRIMARY KEY,
@@ -73,7 +73,7 @@ insert
73
73
In SQL, `SELECT` retrieves data from tables. In TypeQL, `match` finds data by pattern, and `fetch` or `select` projects the results.
74
74
75
75
**SQL:**
76
-
[sql]
76
+
[,sql]
77
77
----
78
78
SELECT name, email FROM person;
79
79
----
@@ -108,7 +108,7 @@ fetch {
108
108
SQL's `WHERE` clause filters rows. In TypeQL, constraints are part of the `match` pattern.
109
109
110
110
**SQL:**
111
-
[sql]
111
+
[,sql]
112
112
----
113
113
SELECT name FROM person WHERE age > 25;
114
114
----
@@ -140,7 +140,7 @@ SQL uses `AND` (conjunction) and `OR` (disjunction) in `WHERE` clauses.
140
140
In TypeQL, statements and composite patterns are combined an implicit `and` by default: `;` and `,` both are implicit conjunctions. An `or` pattern is used to explicitly represent a disjunction.
141
141
142
142
**SQL with AND:**
143
-
[sql]
143
+
[,sql]
144
144
----
145
145
SELECT name FROM person WHERE age > 25 AND email LIKE '%@example.com';
146
146
----
@@ -157,7 +157,7 @@ select $name;
157
157
----
158
158
159
159
**SQL with OR:**
160
-
[sql]
160
+
[,sql]
161
161
----
162
162
SELECT name FROM person WHERE age < 20 OR age > 30;
163
163
----
@@ -182,7 +182,7 @@ The type of JOIN determines which entities are included in the results when rela
182
182
An INNER JOIN returns only rows where there is a match in both tables. In TypeQL, this is the default behavior when matching relations.
183
183
184
184
**SQL:**
185
-
[sql]
185
+
[,sql]
186
186
----
187
187
SELECT p.name, e.id
188
188
FROM person p
@@ -207,7 +207,7 @@ A LEFT JOIN returns all rows from the left table (person), and matching rows fro
207
207
If there's no match, NULL values are returned for the right table columns. In TypeQL, use the `try` construct for optional matching.
208
208
209
209
**SQL:**
210
-
[sql]
210
+
[,sql]
211
211
----
212
212
SELECT p.name, e.id
213
213
FROM person p
@@ -235,7 +235,7 @@ A RIGHT JOIN returns all rows from the right table (employment), and matching ro
235
235
If there's no match, NULL values are returned for the left table columns. In TypeQL, reverse the pattern to start from the right side.
236
236
237
237
**SQL:**
238
-
[sql]
238
+
[,sql]
239
239
----
240
240
SELECT p.name, e.id
241
241
FROM person p
@@ -264,7 +264,7 @@ A CROSS JOIN returns the Cartesian product of two tables - every row from the fi
264
264
In TypeQL, match entities from both types independently without requiring any relation between them.
265
265
266
266
**SQL:**
267
-
[sql]
267
+
[,sql]
268
268
----
269
269
SELECT p.name, e.id
270
270
FROM person p
@@ -294,7 +294,7 @@ Full outer joins do not translate well to TypeQL. If your application requires a
294
294
=== INSERT → insert
295
295
296
296
**SQL:**
297
-
[sql]
297
+
[,sql]
298
298
----
299
299
INSERT INTO person (name, email, age) VALUES ('David', 'david@example.com', 28);
300
300
----
@@ -308,7 +308,7 @@ insert
308
308
----
309
309
310
310
**SQL INSERT with SELECT:**
311
-
[sql]
311
+
[,sql]
312
312
----
313
313
INSERT INTO employment (employee_id, employer_id)
314
314
SELECT p.id, c.id
@@ -330,7 +330,7 @@ insert
330
330
=== UPDATE → update
331
331
332
332
**SQL:**
333
-
[sql]
333
+
[,sql]
334
334
----
335
335
UPDATE person SET email = 'newemail@example.com' WHERE name = 'Alice';
336
336
----
@@ -353,7 +353,7 @@ It automatically deletes the old value and inserts the new one.
SELECT name, age FROM person ORDER BY age DESC, name ASC;
466
466
----
@@ -478,7 +478,7 @@ select $name, $age;
478
478
=== LIMIT and OFFSET
479
479
480
480
**SQL:**
481
-
[sql]
481
+
[,sql]
482
482
----
483
483
SELECT name FROM person ORDER BY name LIMIT 10 OFFSET 20;
484
484
----
@@ -507,7 +507,7 @@ If we did `limit`, then `order`, we would `limit` the sorted answers to the firs
507
507
=== DISTINCT → distinct
508
508
509
509
**SQL:**
510
-
[sql]
510
+
[,sql]
511
511
----
512
512
SELECT DISTINCT age FROM person;
513
513
----
@@ -525,7 +525,7 @@ distinct;
525
525
=== Subqueries → functions
526
526
527
527
**SQL:**
528
-
[sql]
528
+
[,sql]
529
529
----
530
530
SELECT name FROM person
531
531
WHERE age > (SELECT AVG(age) FROM person);
@@ -548,7 +548,7 @@ select $avg, $name, $age;
548
548
=== UNION → or patterns
549
549
550
550
**SQL:**
551
-
[sql]
551
+
[,sql]
552
552
----
553
553
SELECT name FROM person WHERE age < 25
554
554
UNION
@@ -585,7 +585,7 @@ Both are valid!
585
585
=== EXISTS → not { not { ... } }
586
586
587
587
**SQL:**
588
-
[sql]
588
+
[,sql]
589
589
----
590
590
SELECT name FROM person p
591
591
WHERE EXISTS (
@@ -611,7 +611,7 @@ _This will be improved in future versions of TypeQL!_
611
611
=== IN → multiple value constraints
612
612
613
613
**SQL:**
614
-
[sql]
614
+
[,sql]
615
615
----
616
616
SELECT name FROM person WHERE age IN (25, 30, 35);
617
617
----
@@ -639,7 +639,7 @@ select $name;
639
639
=== LIKE → like (regex)
640
640
641
641
**SQL:**
642
-
[sql]
642
+
[,sql]
643
643
----
644
644
SELECT name FROM person WHERE email LIKE '%@example.com';
645
645
----
@@ -657,7 +657,7 @@ select $name;
657
657
=== NULL checks → not { ... }
658
658
659
659
**SQL:**
660
-
[sql]
660
+
[,sql]
661
661
----
662
662
SELECT name FROM person WHERE email IS NULL;
663
663
----
@@ -676,7 +676,7 @@ select $name;
676
676
----
677
677
678
678
**SQL:**
679
-
[sql]
679
+
[,sql]
680
680
----
681
681
SELECT name FROM person WHERE email IS NOT NULL;
682
682
----
@@ -726,7 +726,7 @@ match $avg_age > 29;
726
726
In SQL, relationships are represented through foreign keys. In TypeQL, relationships are explicit relations that can have their own attributes and connect multiple entities.
0 commit comments