SQL言語との比較
このページでは次の表persons,lessons,unitsに対する様々な問い合わせをSQL言語とBraid言語の両方で記述します。このページの内容はSQL言語を知っている方がBraid言語とその表現力を理解するのに役立ちます。
persons
name | age | height | weight |
---|
haruharu | 16 | 1.581 | 45.9 |
chihachiha | 15 | 1.622 | 41.8 |
yukiyuki | 16 | 1.543 | 40.7 |
yayoyayo | 13 | 1.454 | 37.6 |
ritsuritsu | 18 | 1.565 | 43.5 |
azuazu | 20 | 1.686 | 48.4 |
ioio | 14 | 1.507 | 39.3 |
makomako | 16 | 1.578 | 42.2 |
amimami | 12 | 1.499 | 39.1 |
lessons
name | kind | score |
---|
haruharu | voice | 68 |
haruharu | pose | 78 |
chihachiha | lyric | 80 |
chihachiha | dance | 70 |
yukiyuki | expression | 68 |
yukiyuki | voice | 78 |
yayoyayo | pose | 84 |
yayoyayo | lyric | 74 |
ritsuritsu | dance | 64 |
ritsuritsu | expression | 74 |
azuazu | voice | 70 |
azuazu | pose | 60 |
ioio | lyric | 72 |
ioio | dance | 82 |
makomako | expression | 78 |
makomako | voice | 68 |
amimami | pose | 76 |
amimami | lyric | 86 |
units
id | name |
---|
1 | haruharu |
2 | haruharu |
1 | chihachiha |
2 | yukiyuki |
3 | yukiyuki |
3 | yayoyayo |
3 | ritsuritsu |
1つの表に対する絞込
まずは列ageを条件とした単純な絞込を行います。この例では入力と出力の形が同じです。どちらも簡潔ですが、SQL言語は特に簡潔です。
SQL言語
select * from
persons
where
age <= 16
Braid言語
each {
person in persons,
person.age <= 16
} into person
出力
name | age | height | weight |
---|
haruharu | 16 | 1.581 | 45.9 |
chihachiha | 15 | 1.622 | 41.8 |
yukiyuki | 16 | 1.543 | 40.7 |
yayoyayo | 13 | 1.454 | 37.6 |
ioio | 14 | 1.507 | 39.3 |
makomako | 16 | 1.578 | 42.2 |
amimami | 12 | 1.499 | 39.1 |
さらに結果の表に列bmiを付け加えます。この例では入力と出力の形が異なるため前の例より複雑になりますが、まだまだどちらも簡潔です。
SQL言語
select
name as name,
age as age ,
weight / (height * height) as bmi
from
persons
where
age <= 16
Braid言語
each {
person in persons,
person.age <= 16
} into {
name = person.name,
age = person.age,
bmi = person.weight / (person.height * person.height)
}
出力
name | age | bmi |
---|
haruharu | 16 | 18.4 |
chihachiha | 15 | 15.9 |
yukiyuki | 16 | 17.1 |
yayoyayo | 13 | 17.8 |
ioio | 14 | 17.3 |
makomako | 16 | 16.9 |
amimami | 12 | 17.4 |
さらに列bmiを条件とした絞込を行います。まだまだどちらも簡潔です。SQL言語のhaving句は出力の行を生成してから絞込を行うため、性能に悪い影響を与える可能性があります。
SQL言語
select
name as name,
age as age ,
weight / (height * height) as bmi
from
persons
where
age <= 16
having
bmi <= 17
Braid言語
each {
person in persons,
person.age <= 16,
bmi = person.weight / (person.height * person.height),
bmi <= 17
} into {
name = person.name,
age = person.age ,
bmi = bmi
}
出力
name | age | bmi |
---|
chihachiha | 15 | 15.9 |
makomako | 16 | 16.9 |
1つの表に対する集計
表lessonsの列scoreの最大値と最小値を求めます。
SQL言語
SELECT
MAX(score) AS maxScore,
MIN(score) AS minScore
FROM
lessons
Braid言語
lessons group() {
maxScore = maxInteger32(score),
minScore = minInteger32(score)
}
さらに列kindの値でグループ化します。
SQL言語
SELECT
kind AS kind ,
MAX(score) AS maxScore,
MIN(score) AS minScore
FROM
lessons
GROUP BY
kind
Braid言語
lessons group(kind) {
maxScore = maxInteger32(score),
minScore = minInteger32(score)
}
出力
kind | maxScore | minScore |
---|
voice | 78 | 68 |
pose | 84 | 60 |
lyric | 86 | 72 |
dance | 82 | 64 |
expression | 78 | 68 |
複数の表に対する絞込
表personsと表lessonsを結合して、表personsの列ageと、表lessonsの列scoreで絞込を行います。
SQL言語
SELECT
persons.name AS name,
persons.age AS age ,
lessons.kind AS kind,
lessons.score AS score
FROM
persons INNER JOIN lessons ON persons.name = lessons.name
WHERE
persons.age < 15 AND lessons.score >= 80
Braid言語
each {
person in persons,
person.age < 15,
lesson in lessons,
lesson.score >= 80,
person.name == lesson.name
} into {
name = person.name,
age = person.age ,
kind = lesson.kind,
score = lesson.score
}
出力
name | age | kind | score |
---|
yayoyayo | 13 | pose | 84 |
ioio | 14 | dance | 82 |
amimami | 12 | lyric | 86 |
複数の表に対する集計
表personsと表unitsを結合して、表personsの列heightの最大値を、表unitsの列idの値でグループ化して求めます。
SQL言語
SELECT
units.id AS id,
MAX(persons.height) AS maxHeight
FROM
persons INNER JOIN units ON persons.name = units.name
GROUP BY
units.id
Braid言語
each {
person in persons,
unit in units,
person.name == unit.name,
id = unit.id
} group(id) {
maxHeight = maxInteger32(person.height)
}
出力
id | maxHeight |
---|
1 | 1.622 |
2 | 1.581 |
3 | 1.565 |