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&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 0202&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 0302&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 04
Empr_dep 01&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 0101&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 0202&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 03
Solução-- TABELA EMPRESACREATE TABLE EMPRESA&nbsp;(&nbsp;COD CHAR(2) NOT NULL,&nbsp;NOME VARCHAR(20) NOT NULL,&nbsp;PRIMARY KEY (COD)&nbsp;);
-- TABELA EMPREGADOSCREATE TABLE EMPREGADOS(COD CHAR(2) NOT NULL,NOME VARCHAR(20) NOT NULL,SALARIO REAL PRIMARY KEY (COD));
-- TABELA DEPENDENTESCREATE TABLE DEPENDENTES(COD CHAR(2) NOT NULL,NOME VARCHAR(20) NOT NULL,PRIMARY KEY (COD));
-- RELACIONAMENTO EMPRESAS X EMPREGADOSCREATE TABLE EMP_EMP(COD_EMP CHAR(2) NOT NULL,&nbsp;COD_FUN CHAR(2) NOT NULL,&nbsp;PRIMARY KEY (COD_EMP,COD_FUN),&nbsp;FOREIGN KEY (COD_EMP) REFERENCES EMPRESA(COD),&nbsp;FOREIGN KEY (COD_FUN) REFERENCES EMPREGADOS(COD)&nbsp;);
-- RELACIONAMENTO EMPREGADOS X DEPENDENTESCREATE TABLE EMP_DEP(&nbsp;COD_FUN CHAR(2) NOT NULL,&nbsp;COD_DEP CHAR(2) NOT NULL,&nbsp;PRIMARY KEY (COD_FUN,COD_DEP),&nbsp;FOREIGN KEY (COD_FUN) REFERENCES EMPREGADOS(COD),&nbsp;FOREIGN KEY (COD_DEP) REFERENCES DEPENDENTES(COD)&nbsp;);-- ELEMENTOS DA TABELA EMPRESAINSERT INTO EMPRESA VALUES ('01','EMPRESA_A')INSERT INTO EMPRESA VALUES ('02','EMPRESA_B')
-- ELEMENTOS DA TABELA EMPREGADOSINSERT INTO EMPREGADOS VALUES ('01','FUNCIONARIO_A',200)INSERT INTO EMPREGADOS VALUES ('02','FUNCIONARIO_B',130)INSERT INTO EMPREGADOS VALUES ('03','FUNCIONARIO_C',400)INSERT INTO EMPREGADOS VALUES ('04','FUNCIONARIO_D',130)
-- ELEMENTOS DA TABELA DEPENDENTESINSERT INTO DEPENDENTES VALUES ('01','DEPENDE_A')INSERT INTO DEPENDENTES VALUES ('02','DEPENDE_B')INSERT INTO DEPENDENTES VALUES ('03','DEPENDE_C')
-- ELEMENTOS DA TABELA EMP_EMPINSERT INTO EMP_EMP VALUES ('01','01')INSERT INTO EMP_EMP VALUES ('01','02')INSERT INTO EMP_EMP VALUES ('02','03')INSERT INTO EMP_EMP VALUES ('02','04')
-- ELEMENTOS DA TABELA EMP_DEPINSERT INTO EMP_DEP VALUES ('01','01')INSERT INTO EMP_DEP VALUES ('01','02')INSERT INTO EMP_DEP VALUES ('01','03')
Recuperação &nbsp;Operadores de comparação (=,&gt;,&lt;,&lt;=,&gt;=,&lt;&gt;,!=,!&lt;, e !&gt;)Amplitude (BETWEEN and NOT BETWEEN)Lista (IN and NOT IN)Combinação de Strings (LIKE and NOT LIKE)Valores desconhecidos (IS NULL e IS NOT NULL)Combinações destes (AND, OR)Negações (NOT)
SELECT select_list&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; FROM table_list&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; WHERE column_name IS [NOT] NULL
SELECT select_list&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; FROM table_list&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; WHERE expression comparison_operator expression&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
-- OBTER AS EMPRESAS DOS FUNCIONÁRIOSSELECT&nbsp; COD, NOME FROM EMPRESA, EMP_EMP WHERE&nbsp; EMPRESA.COD = EMP_EMP.COD_FUN
&nbsp;SELECT select_list&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; FROM table_list&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; WHERE [NOT] expression {AND|OR}[NOT] expression
-- OBTER O NOME DO FUNCIONARIO DE CODIGO 01 SELECT&nbsp; EMPREGADOS.NOME FROM EMPREGADOS, EMP_EMP WHERE&nbsp; EMPREGADOS.COD = EMP_EMP.COD_FUN ANDEMP_EMP.COD_FUN = '01'
-- FORMA ERRADA DE BUSCA INFORMAÇÃOSELECT&nbsp; COD_EMP, EMPRESA.NOME AS EMPRESA,EMPREGADOS.NOME AS FUNCIONARIO FROM EMP_EMP, EMPRESA,EMPREGADOS WHERE&nbsp; EMP_EMP.COD_EMP = EMPRESA.COD
SELECT select_list&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; FROM table_list&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; WHERE expression [NOT] LIKE ‘string’
Outras opções Seus dados existem pessoas com nome 'Sousa' ou 'Souza', poderá utilizar: LIKE '%sou_a%'
Utilizar cochetes para delimitar uma faixa, ex: Procurar por nomes que iniciam pelas letras &nbsp;&nbsp;F e XSELECT * FROM EMPREGADOS WHERE NOME LIKE '[FX]%'
ex: Procurar nomes de uma letra até outra, ex: Letra A até a Letra FSELECT * FROM EMPREGADOS WHERE NOME LIKE '[A-F]%'
ex: Procurar nomes que não comecem com a Letra FSELECT * FROM EMPREGADOS WHERE NOME LIKE '[^F]%'
-- Nome da Empresa do Empregado 3SELECT EMPRESA.NOME AS EMPRESA&nbsp; FROM EMPRESAWHERE COD IN ( SELECT COD_EMP FROM EMP_EMP WHERE&nbsp; COD_FUN='03')
-- OBTER OS CODIGOS DOS DEPENDENTES E OS NOMES-- DOS EMPREGADOS RELACIONADOS COM O DEPENDENTESSELECT&nbsp; COD_DEP,COD_FUN,NOME FROM&nbsp; EMP_DEP,EMPREGADOS WHERE EMP_DEP.COD_FUN = EMPREGADOS.COD
-- OBTER O NOME DO DEPENDENTE DO CODIGO '02'SELECT NOMEFROM DEPENDENTESWHERE COD IN (SELECT COD_DEP FROM EMP_DEP WHERE COD_DEP = '02')
-- OBTER OS NOMES DOS DEPENDENTESSELECT NOMEFROM DEPENDENTESWHERE COD IN (SELECT COD_DEP FROM EMP_DEP WHERE COD_DEP = DEPENDENTES.COD)
-- OBTER OS NOMES DO EMPREGADOS QUE COMEÇAM COM-- A LETRA FSELECT NOMEFROM EMPREGADOS WHERE NOME LIKE 'F%'
-- OBTER OS NOMES DO EMPREGADOS QUE TEM A SUBCADEIA-- DSELECT NOMEFROM EMPREGADOS WHERE NOME LIKE '%D%'

-- OBTER OS NOMES DO EMPREGADOS QUE NÃO TEM A SUBCADEIA-- DSELECT NOMEFROM EMPREGADOS WHERE NOME NOT LIKE '%D%'
-- OBTER OS DADOS DO FUNCIONARIOS COM-- SALARIOS ACIMA DE 130SELECT *FROM EMPREGADOS WHERE SALARIO &gt; 130
SELECT select_list&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; FROM table_list&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; WHERE expression [NOT] BETWEEN expression AND expression
-- OBTER OS DADOS DO FUNCIONARIOS COM-- SALARIOS ENTRE 45&nbsp; E 130 SELECT *FROM EMPREGADOS WHERE SALARIO BETWEEN 45 AND 130
OU
-- OBTER OS DADOS DO FUNCIONARIOS COM-- SALARIOS ENTRE 45&nbsp; E 130 SELECT *FROM EMPREGADOS WHERE SALARIO &gt;= 45 AND SALARIO &lt;= 130
-- OBTER OS DADOS DO FUNCIONARIOS EM ORDEM-- ASCENDENTE DE SALARIOSELECT *FROM EMPREGADOS ORDER BY&nbsp; SALARIO
-- OBTER OS DADOS DO FUNCIONARIOS EM ORDEM-- DESCENDENTE DE SALARIOSELECT *FROM EMPREGADOS ORDER BY&nbsp; SALARIO&nbsp; DESC

-- OBTER A MEDIA DOS SALARIOSSELECT AVG(SALARIO) AS MEDIA FROM EMPREGADOS
-- OBTER A MEDIA DOS SALARIOS &gt; 130SELECT AVG(SALARIO) AS MEDIA FROM EMPREGADOSWHERE SALARIO &gt; 130
-- OBTER O MAIOR SALARIO SELECT MAX(SALARIO) AS MAIOR FROM EMPREGADOS
-- OBTER A QUANTIDADE DE TUPLAS SELECT COUNT(SALARIO) AS QTD FROM EMPREGADOS-- OBTER A SOMA DOS SALARIOS SELECT SUM(SALARIO) AS SOMA FROM EMPREGADOS
-- REAJUSTAR O SALARIO DOS EMPREGADOS-- QUE TEM UM SALARIO IGUAL A 400UPDATE EMPREGADOS SET SALARIO = SALARIO * 1.23WHERE SALARIO = 400

Até a próxima e cuide-se bem.