Sie sind auf Seite 1von 11

SQL estndar Introduccin

SQL quiere decir "Strutured Query Language", que lo podemos traducir como lenguaje estructurado para consultas. El SQL o Sequel, como le llaman algunos, es el lenguaje ms importante para el manejo de bases de datos relacionales. El SQL es un ejemplo de unos de los lenguajes interactivos que forman parte del DML (Data Manipulation Language)

Sintaxis
La sintaxis general de una instruccin de SQL es: SELECT ... FROM ... WHERE ... La instruccin de SELECT combina las tres operaciones relacionales bsicas: Proyeccin ( ), Seleccin ( ) y "Join" ( |x| ) Por ejemplo si tenemos la base de datos con relaciones R y S que tienen los siguientes esquemas: R(A,B,C) y S(C,D,E). Note: El dominio C es el atributo o campo que une las relaciones. Para la expresin relacional a,b,c (R) en SQL se escribira la instruccin: SELECT a,b,c FROM R; El "semicolon" (;) al final lo lleva si estamos trabajando en SQL de Oracle. Si tenemos la expresin relacional

( condicin (R)) en SQL se escribira la instruccin:


a,b,c

SELECT a,b,c FROM R WHERE condicin; Para la expresin relacional a,b,d (R|x|S) en SQL se escribira la instruccin:

SELECT a,b,d FROM R,S WHERE R.c=S.c; El "join" queda establecido a travs de la condicin R.c=S.c. Para las expresiones en que hay una condicin y un "join" tenemos que unir ambas condiciones en la clasula WHERE, veamos el ejemplo: Si tenemos la expresin relacional

( condicin (R|x|S)) en SQL se escribira la instruccin:


a,b,d

SELECT a,b,c FROM R WHERE R.c=S.c AND condicin;

Ejemplos con base de datos Chazy


A continuacin se presentan aspectos de la sintaxis y la semntica de SQL a travs de ejemplos. 1. Listar el Id, nombre y costo de todos los softwares en la base de datos.

PACKID,PACKNAME,PACKCOST

( PACKTYPE=Database (PACKAGE)

SELECT packid, packname,packcost FROM package WHERE packtype = 'Database';

2. Listar toda los datos de la tabla de empleados

EMPNUM,EMPNAME,PHONE (EMPLOYEE)
SELECT * FROM employee; NOTA: Se usa el asterisco para indicar que se quieren todos los campos de la tabla.
3. Cul es el nombre del empleado 124?

EMPNAME( EMPNUM=124 (EMPLOYEE))


SELECT empname FROM employee WHERE empnum=124;

4. Hallar el id y el nombre de todos los paquetes que son Database.

PACKID,PACKNAME( PACKTYPE=Database (PACKAGE))


SELECT packid,packname FROM package WHERE Packtype = 'Database";
5. Listar el nombre de todos los paquetes que son Database y tienen costo de ms de $400.

PACKNAME

( PACKTYPE=Database AND PACKCOST > 400 (PACKAGE))

SELECT packname FROM package WHERE packtype= 'Database' AND packcost > 400;

6. Listar los nombres de todos los paquetes que no son Database

PACKNAME( PACKTYPE<>Database (PACKAGE))


SELECT packname FROM package WHERE packtype != 'Database';

Tambin se puede escribir:

SELECT packname FROM package WHERE packtype <> 'Database';

7. Listar el id y nombre de todos los paquetes que tienen costo mayor a $250.

PACKID,PACKNAME( PACKCOST > 250 (PACKAGE))

SELECT packid,packname FROM package WHERE packcost >250;

8. Para cada PC, listar el tagnumber, el computerid con el nmero y nombre de empleado que la tiene asignada.

TAGNUMBER,COMPUTERID,EMPNUMBER,EMPNAME (PC |X| EMPLOYEE)

SELECT tagnumber,computerid,pc.empnumber,empname FROM pc,employee WHERE pc.empnumber = employee.empnumber; Tambin se puede escribir de otra forma usando alias para las tablas. Un alias quiere decir un nombre equivalente que se establece en la clasula FROM de la consulta. Veamos como se escribira: SELECT tagnumber,computerid,a.empnumber,empname FROM pc a,employee b WHERE a.empnumber = b.empnumber;
9. Para cada PC, listar el tagnumber, el computerid, el nombre de la manufacturera y el nmero y nombre del empleado que tiene asignada esa PC.

TAGNUMBER,COMPID,MFGNAME,EMPNUM,EMPNAME (COMPUTER |X| PC |X| EMPLOYEE)

SELECT tagnumber,a.compid,mfgname, b.empnumber,empname FROM computer a, pc b,employee c WHERE a.compid = b.compid AND b.empnumber = c.empnumber;
10. Para cada PC con procesador 486DX, listar el tagnumber, nombre del manufacturero y nombre del empleado a que esta asignada.

TAGNUMBER,MFGNAME,EMPNAME (

PROCTYPE= 486DX (COMPUTER |X| PC |X| EMPLOYEE))

SELECT tagnumber,mfgname,empname FROM computer a, pc b,employee c WHERE a.compid = b.compid AND b.empnumber = c.empnumber AND proctype= '486DX';

11. Listar toda la informacin respecto al package SS11.

PACKID,PACKNAME,PACKVERSION,PACKCOST

( PACKID=SS11 (PACKAGE))

SELECT packid, packtype, packname, packversion, packcost FROM package WHERE packid = 'SS11';
12. Listar el compid y nombre de manufacturero de todas las computadoras con procesador 486DX que no fueron asignadas para la casa.

COMPID,MFGNAME (

PROCTYPE= 486DX AND LOCATION <>Home (COMPUTER |X| PC))

SELECT a.compid, mfgname FROM computer a, pc b WHERE a.compid = b.compid AND proctype= '486DX' AND location != 'Home';
13. Listar el nmero y nombre de los empleados que tienen PC asignadas para la casa y para ventas.

EMPNUM,EMPNAME(

LOCATION ='Sales' (PC |x| EMPLOYEE)

interseccin
EMPNUM,EMPNAME( LOCATION ='Home' (PC |x| EMPLOYEE)

(SELECT a.empnumber,empname FROM pc a,employee b WHERE a.empnumber = b.empnumber AND location = 'Sales') INTERSECT (SELECT a.empnumber,empname FROM pc a,employee b WHERE a.empnumber = b.empnumber AND location = 'Home');

Ejemplos con base de datos Summit Sporting Goods


Using Summit Sporting Goods database write a SELECT statement for:

1. Display all information held about regions. Select * from s_region; 2. Display all information held about departments. Select * from s_dept; 3. Display departments names in the S_DEPT table. Select name from s_dept; 4. Display job titles in the employee table. Select title from s_dept; 5. Display the department ID, and first and last name for all employees in department 41. Select dept_id, first_name, last_name from s_emp; where dept_id = 41; 6. Display the employee ID, first and last name, and salary for employee 1. Select id, first_name, last_name, salary from s_emp where id = 1; 7. Display the first and last name, and title for the employee named "Magee". Select first_name, last_name, title from s_emp where last_name = 'Magee'; 8. Display the last name, user ID, and start date for all employees hired on March 4, 1990. Select last_name, userid, start_date From s_emp Where start_date = '04-mar-90';

9 .Display the first and last name and start date of employees whose start date is in between may 9, 1991 and June 17, 1991, inclusive. Select first_name, last_name, start_date From s_emp Where start_date BETWEEN '09-may-91' AND '17-jun-91'; 10 .Display the first and last name and salary of employees making a salary between $1,500 and $3,000 per month. Select first_name, last_name, salary From s_emp Where salary > 1500 and salary < 3000; 11. Display the department ID, name, and the region ID of departments not in region 4 or 5. Select id, name, region_id From s_dept Where region_id IN (4,5); 12. Display the last name, salary and department ID of employees in department 42 who earn at least $1,200. Select last_name, salary, dept_id From s-emp Where dept_id = 42 and salary >= 1200; 13. Display the last name, salary, department ID, and title of employees in department 41, and display anyone who is a "Stock Clerk". Select last_name, salary, dept_id, title From s_emp Where dept_id = 41 or title = 'Stock Clerk'; 14. Display the last name of employees who have the same title as Smith. Select last_name From s_emp Where title IN (select title From s_emp Where last_name = 'Smith'); 15. Display the last name, job title and department ID for all employees in the same department as Biri.

Select last_name, title, dept_id From s_emp Where dept_id IN (select dept_id From s_emp Where last_name = 'Biri'); 16. Display the employee ID, last name, and department ID for all employees who work in a department with any employee named Patel. Select id, last_name, dept_id From s_emp Where dept_id IN (select dept_id From s_emp Where last_name = 'Patel'); 17. Display the customer ID, name and credit rating for all customers that are located in North America region or have Nagayama as their sales representative. Select c.id , c.name, c.credit_rating From s_customer c, s_emp e, s_region r Where ( c.region_id = r.id and c.sales_rep_id = e.id) AND ( r.name = 'North America' or e.last_name = 'Nagayama'); 18. Display employee ID, last and first name, department ID, and department name for all employees. Select e.id, last_name, first_name, d.id, name From s_emp e, s_dept d Where dept_id = d.id; 19. Display department ID, department name, region ID, and region name for all departments. Select d.id, d.name, r.id, r.name From s_dept d, s_region r Where region_id = r.id; 20. Display department ID, department name, region ID, and region name for departments10, 31 and 41. Select d.id, d.name, r.id, r.name From s_dept d, s_region r Where region_id = r.id AND d.id IN (10,31,41);

21. Display the distinct job titles. Select distinct title From s_emp; 22. Display each unique department name with a heading of "Diffrerent Departments". Select distinct name "Different Departments" From s_dept; 23. Display all employee names with a heading of "EMPLOYEES". Select last_name EMPLOYEES From s_emp; 24.Display the distinct department names and Ids of those departments whose Ids are not in 30 to 39 range. Select distinct id, name From s_dept Where id NOT BETWEEN 30 AND 39; 25. Display the last name, department ID, and start date for employees whose start date was not in 1991. Select last_name, dept_id, start_date From s_emp Where start_date NOT BETWEEN '01-jan-91' AND '31-dec-91'; 26. Display the department ID, name, and region ID of departments not in region 4 or 5. Select id, name, region_id From s_dept Where region_id NOT IN (4,5); 27. Display the last name of employees whose last name begin with "M" . Select last_name From s_emp Where last_name LIKE 'M%'; 28. Display the last name and start date for employees who started with the company in 1991.

Select last_name, start_date From s_emp Where start_date LIKE '%91'; 29. Display the last name and user ID of employees that an "a" in their user ID. Select last_name, userid From s_emp Where userid LIKE '%a%'; 30. Display the last name of employees whose last name contains five letters, begins with an "S', and ends with an "h". Select last_name From s_emp Where last_name LIKE 'S ___h'; NOTA: Se escribe el smbolo de "underscore" por cada letra. En el ejercicio hay tres smbolos de "underscore" unidos. 31. Display the customer ID, name, and credit rating of customers who do not have a sales representative. Select id, name, credit_rating From s_customer Where sales_rep_id IS NULL; 32. Display the last name, title, and commission percent for employees who earn a commission percent. Select last_name, title, commission_pct From s_emp Where commission_pct IS NOT NULL; 33. Display the last names and salaries of employees in department 45 in ascending order by salary. Select last_name, salary From s_emp Where dept_id = 45 Order by salary; 34. Display the region ID, department ID, and name of each department in order by region ID and then by department ID.

Select region_id, id, name From s_dept Order by region_id, id;

Das könnte Ihnen auch gefallen