SQL言語との比較

このページでは次の表persons,lessons,unitsに対する様々な問い合わせをSQL言語とBraid言語の両方で記述します。このページの内容はSQL言語を知っている方がBraid言語とその表現力を理解するのに役立ちます。

persons
nameageheightweight
haruharu161.58145.9
chihachiha151.62241.8
yukiyuki161.54340.7
yayoyayo131.45437.6
ritsuritsu181.56543.5
azuazu201.68648.4
ioio141.50739.3
makomako161.57842.2
amimami121.49939.1
lessons
namekindscore
haruharuvoice68
haruharupose78
chihachihalyric80
chihachihadance70
yukiyukiexpression68
yukiyukivoice78
yayoyayopose84
yayoyayolyric74
ritsuritsudance64
ritsuritsuexpression74
azuazuvoice70
azuazupose60
ioiolyric72
ioiodance82
makomakoexpression78
makomakovoice68
amimamipose76
amimamilyric86
units
idname
1haruharu
2haruharu
1chihachiha
2yukiyuki
3yukiyuki
3yayoyayo
3ritsuritsu

1つの表に対する絞込

まずは列ageを条件とした単純な絞込を行います。この例では入力と出力の形が同じです。どちらも簡潔ですが、SQL言語は特に簡潔です。

SQL言語
select * from
  persons
where
  age <= 16
Braid言語
each {
  person in persons,
  person.age <= 16
} into person
出力
nameageheightweight
haruharu161.58145.9
chihachiha151.62241.8
yukiyuki161.54340.7
yayoyayo131.45437.6
ioio141.50739.3
makomako161.57842.2
amimami121.49939.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)
}
出力
nameagebmi
haruharu1618.4
chihachiha1515.9
yukiyuki1617.1
yayoyayo1317.8
ioio1417.3
makomako1616.9
amimami1217.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
}
出力
nameagebmi
chihachiha1515.9
makomako1616.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)
}
出力
maxScoreminScore
8660

さらに列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)
}
出力
kindmaxScoreminScore
voice7868
pose8460
lyric8672
dance8264
expression7868

複数の表に対する絞込

表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
}
出力
nameagekindscore
yayoyayo13pose84
ioio14dance82
amimami12lyric86

複数の表に対する集計

表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)
}
出力
idmaxHeight
11.622
21.581
31.565

カテゴリー: ニュース