Vous êtes sur la page 1sur 12

EXEMPLE DE INTEROGARI IN SQL

COMPUNERE CU WHERE
2 tabele.

SELECT Facturi.nrfact, Facturi.datafact,


Facturi.codfiscal, [Continut Factura].codisbn,
[Continut factura].cantitate,
[Continut Factura].pretf, [Continut
Factura].cantitate*[Continut Factura].pretf AS
valoare
FROM Facturi, [Continut Factura]

WHERE Facturi.nrfact=[Continut
Factura].nrfact;
3 tabele
SELECT Facturi.nrfact, Facturi.datafact,
Facturi.codfiscal, [Continut factura].codisbn,
[Continut factura].cantitate, [Continut
factura].pretf, [Continut
factura].cantitate*[Continut factura].pretf AS
valoare, Clienti.codfiscal, Clienti.denumirecl
FROM Facturi, [Continut factura], Clienti
WHERE Facturi.nrfact=[Continut
factura].nrfact And
Clienti.codfiscal=Facturi.codfiscal;
COMPUNERE CU JOIN
a)
SELECT facturi.nrfact, facturi.datafact,
facturi.codfiscal, [continut factura].codisbn,
[continut factura].cantitate, [continut
factura].pretf, [continut
factura].cantitate*[continut factura].pretf AS
valoare

FROM facturi INNER JOIN [continut factura] ON


facturi.nrfact=[continut factura].nrfact;

b)
SELECT facturi.nrfact, facturi.datafact,
facturi.codfiscal, [continut factura].codisbn,
[continut factura].cantitate, [continut
factura].pretf, [continut
factura].cantitate*[continut factura].pretf AS
valoare
FROM facturi LEFT JOIN [continut factura] ON
facturi.nrfact=[continut factura].nrfact;

c)
SELECT facturi.nrfact, facturi.datafact,
facturi.codfiscal, [continut factura].codisbn,
[continut factura].cantitate, [continut
factura].pretf, [continut
factura].cantitate*[continut factura].pretf AS
valoare

FROM facturi RIGHT JOIN [continut factura] ON


facturi.nrfact=[continut factura].nrfact;

SELECT DISTINCT Max(a5) AS


[a5_Maxim], Min(a5) AS [a5_Minim],
Avg(a5) AS [a5_Medie] FROM A;
SELECT COUNT(*) AS [Numr de tupluri]
FROM A;

SELECT a1, a2, a3, IIF(a1>=a2;a2-a3;a1-a3)


AS [Rezultat evaluare]
FROM A
WHERE a2 IS NOT NULL;
SELECT DISTINCT ROW a1,a5,a6
FROM A
WHERE a5 > (SELECT a5 FROM A WHERE
a6=text);
SELECT Facturi.[Numar Factura],
[Continut Factura].[Cod ISBN],
Month([Data Facturii]) AS Luna,
Year([Data Facturii]) AS Anul,
[Cantitate]*[Pret f] AS Valoare
FROM Facturi INNER JOIN [Continut Factura]
ON Facturi.[Numar Factura] = [Continut
Factura].[Numar Factura]
WHERE Month([Data Facturii])=3 AND
Year([Data Facturii])=2010;
INSERT INTO [CLIENTI-NOI] ( [COD-CL],
[DEN-CL] )

SELECT CLIENTI.[COD-CL], CLIENTI.[DENCL]


FROM CLIENTI WHERE [COD-CL]=1 OR
[COD-CL]=5;
INSERT INTO FACTURI ( [NR-FACTURA],
[COD-CL], [COD-MARFA], CANTITATE, PRET,
DATA )
SELECT [FACTURI-NOI].[NR-FACTURA],
[FACTURI-NOI].[COD-CL], [FACTURI-NOI].
[COD-MARFA], [FACTURI-NOI].CANTITATE,
[FACTURI-NOI].PRET,
[FACTURI-NOI].DATA FROM [FACTURI-NOI]
WHERE ([facturi-noi].[COD-MARFA]=2)
AND ([FACTURI-NOI].DATA>#3/3/2012#);
TRANSFORM Sum([CLIENTI-MARFURIFACTURI].CANTITATE) AS SumACANTITATE
SELECT [CLIENTI-MARFURI-FACTURI].[DENMARFA]
FROM [CLIENTI-MARFURI-FACTURI]
GROUP BY [CLIENTI-MARFURI-FACTURI].
[DEN-MARFA]

PIVOT [CLIENTI-MARFURI-FACTURI].[DENCL];
DELETE FACTURI.[NR-FACTURA]
FROM FACTURI
WHERE ((FACTURI.[NR-FACTURA])=2);
Sinteza pe clienti
SELECT [CLIENTI-MARFURI-FACTURI].[CODCL], [CLIENTI-MARFURI-FACTURI].[DEN-CL],
Sum([CLIENTI-MARFURIFACTURI].VALOARE) AS SumOfVALOARE
FROM [CLIENTI-MARFURI-FACTURI]
GROUP BY [CLIENTI-MARFURI-FACTURI].
[COD-CL], [CLIENTI-MARFURI-FACTURI].
[DEN-CL];
join
SELECT CLIENTI.[COD-CL], CLIENTI.[DENCL], LIVRARI.[COD-MARFA],
LIVRARI.CANTITATE

FROM CLIENTI RIGHT JOIN LIVRARI ON


CLIENTI.[COD-CL] = LIVRARI.[COD-CLIENT];

UPDATE [facturi-noi] SET PRET = pret*1.2


WHERE (([cod-cl]>1));
SELECT CLIENTI.[COD-CL], CLIENTI.[DENCL], CLIENTI.LOCALITATE
FROM CLIENTI
WHERE
((CLIENTI.LOCALITATE)=[INTRODUCETI
LOCALITATE=]);
SELECT [CodFiscal],
[DenumireCL],Localitate,Telefon
FROM Clienti
UNION SELECT [CodFiscal],
[DenumireCl],Localitate,Telefon
FROM [Clienti Arad]
ORDER BY Localitate, [DenumireCl];

SELECT CLIENTI.[COD-CODFISCAL],
LOCALITATE
FROM CLIENTI
WHERE CLIENTI.[COD-CODFISCAL] IN
(SELECT FACTURI.[COD-CODFISCAL]
FROM FACTURI
WHERE FACTURI.[CODCODFISCAL]=555555555);
Furnizorii care au data contractului 17
aprilie 2012
SELECT DISTINCT [Nrcontract], codfz, DATA
FROM CONTRACTE
WHERE CODFZ IN
(SELECT CODFZ FROM CONTRACTE
WHERE([DATA]= #4/17/2012#));
Furnizorii din Bucuresti care au contract in
anul curent si valoare contract >10000
SELECT FURNIZORI.codfz AS
FURNIZORI_codfz, FURNIZORI.denfz,
FURNIZORI.localitate,
CONTRACTE.NRCONTRACT,
CONTRACTE.DATA, CONTRACTE.VALOARE

FROM FURNIZORI RIGHT JOIN CONTRACTE


ON FURNIZORI.codfz=CONTRACTE.CODFZ
WHERE
(((FURNIZORI.localitate)="bucuresti") And
(year(CONTRACTE.DATA)=year(now())) And
((CONTRACTE.VALOARE)>10000));
SELECT Count(CONTRACTE.NRCONTRACT)
AS nrcontracte,
CONTRACTE.DATA,
Sum(CONTRACTE.VALOARE) AS
TOTALFROM CONTRACTE
GROUP BY CONTRACTE.DATA;

SELECT FURNIZORI.codfz AS
FURNIZORI_codfz, FURNIZORI.denfz,
FURNIZORI.localitate,
CONTRACTE.NRCONTRACT,
CONTRACTE.DATA, CONTRACTE.VALOARE,
CONTRACTE.CODFZ AS CONTRACTE_CODFZ

FROM FURNIZORI RIGHT JOIN CONTRACTE


ON FURNIZORI.codfz = CONTRACTE.CODFZ
WHERE (((FURNIZORI.localitate)="iasi" Or
(FURNIZORI.localitate)="cluj" Or
(FURNIZORI.localitate)="deva") AND
((CONTRACTE.DATA) Between
(#1/3/2011#) And (#3/31/2012#)));