-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSQLQuery_19.sql
78 lines (73 loc) · 1.55 KB
/
SQLQuery_19.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
CREATE DATABASE Exercicio_6
GO
USE Exercicio_6
GO
CREATE TABLE motorista
(
codigo INT NOT NULL,
nome VARCHAR(20) NOT NULL,
idade INT NOT NULL,
naturalidade VARCHAR(30) NOT NULL
PRIMARY KEY(codigo)
)
GO
CREATE TABLE onibus
(
placa VARCHAR(20) NOT NULL,
marca VARCHAR(20) NOT NULL,
ano INT NOT NULL,
descricao VARCHAR(30) NOT NULL
PRIMARY KEY(placa)
)
GO
CREATE TABLE viagem(
codigo INT NOT NULL,
onibus VARCHAR(20) NOT NULL,
motorista INT NOT NULL,
hora_saida INT,
hora_chegada INT,
destino VARCHAR(20)
PRIMARY KEY(codigo)
FOREIGN KEY(onibus) REFERENCES onibus(placa),
FOREIGN KEY(motorista) REFERENCES motorista(codigo)
)
GO
--1)
SELECT CONVERT(CHAR(02), viagem.hora_chegada) + ' h' AS hora_chegada, CONVERT(CHAR(02), viagem.hora_saida) + ' h' AS hora_saida
FROM viagem
--2)
SELECT motorista.nome
FROM motorista
WHERE motorista.codigo IN
(
SELECT viagem.motorista
FROM viagem
WHERE viagem.destino LIKE 'Sorocaba'
)
--3)
SELECT onibus.descricao
FROM onibus
WHERE onibus.placa IN
(
SELECT viagem.onibus
FROM viagem
WHERE viagem.destino LIKE 'Rio de%'
)
--4)
SELECT onibus.descricao, onibus.marca, onibus.ano
FROM onibus
WHERE onibus.placa IN
(
SELECT viagem.onibus
FROM viagem
WHERE viagem.motorista IN
(
SELECT motorista.codigo
FROM motorista
WHERE motorista.nome LIKE 'Luiz Carlos'
)
)
--5)
SELECT motorista.nome, motorista.idade, motorista.naturalidade
FROM motorista
WHERE motorista.idade > 30