SQL Parte 1
SQL Parte 1
Bases de Datos
Contenido
1. Consultas básicas de una sola tabla
Consulta Select-From-Where
Operadores en la clausula Where
2. Consultas Multi-tablas
Join
Aliasing
3. Consultas Anidadas
Tipos de consultas anidadas
Agrupación y Funciones de Agregación
Consulta SQL
producto
SELECT <attribute list>
cartesiano de las
FROM <table list>
relaciones
WHERE <condition on the tables>
condiciones de la forma attr1 op constant/attr2
No elimina duplicados
como el SELECT en el
algebra relacional
⇡<attribute list> <condition> (R1 ⇥ R2 ⇥ · · · ⇥ Rn )
Ejemplo: Product Database
PRODUCT
Name Category Price Manufacter
iPad Tablet $399.00 Apple
Surface Tablet $299.00 Microsoft
Kindle eReader $79.00 Amazon
Macbook Air Laptop $999.99 Apple
Consulta SQL Básica: * SELECTOR
• Selecciona todos los valores de todas las tuplas de
una relación
• Ejemplo:
SELECT * (Product)
FROM Product;
Name Category Price Manufacter
iPad Tablet $399.00 Apple
Surface Tablet $299.00 Microsoft
Kindle eReader $79.00 Amazon
Macbook Air Laptop $999.99 Apple
Selección usando *
Category=‘Tablet’ (Product)
SELECT *
FROM Product
WHERE Category = ‘Tablet’;
Name Category
iPad Tablet
Surface Tablet
Kindle eReader
Macbook Air Laptop
Selección y Proyección
SELECT Name, Category
FROM Product
WHERE Manufactor = ‘Apple’
Name Category
iPad Tablet
Macbook Air Laptop
Detalles de SQL
Category
• SELECT DISTINCT Category Tablet
FROM Product; eReader
Laptop
Detalles de SQL: Condición WHERE
• Operaciones aritméticas: +, -, *, /
Detalles de SQL: Condición WHERE
• Test de pertenencia
• Pattern matching
IN: Test de pertenencia a un conjunto
• Sintaxis:
SELECT <attr list>
FROM <table>
WHERE attr IN (set of values);
Ejemplo de consultas: IN
• Buscar el nombre y el precio de los productos fabricados
por Amazon o Microsoft:
SELECT name, price
FROM Product
WHERE Manufacter IN (‘Amazon’, ‘Microsoft’);
• Soporta 2 comodines
Underscore (_) matches exactly one character
(equivalent to ? in the UNIX shell)
• Sintaxis:
attr IS NULL
• Sintaxis:
SELECT <attribute list>
FROM <table list>
WHERE <condition on the tables>
LIMIT <number of tuples>;
Ejercicios: Company Database (2)
• Comando SQL
• Expresión SQL 1
SELECT fname, lname, address
FROM employee, department
WHERE dname=‘Research’ AND dno = dnumber;
• Expresión SQL 2 join condition
SELECT fname, lname, address
FROM employee JOIN
department ON dno = dnumber
WHERE dname=‘Research’; selection condition
Ejercicios: Company Database (3)
SELECT pno
FROM works_on, dependent
WHERE works_on.essn = dependent.essn
AND name = ‘Alice’
AND relationship = ‘Daughter’;
SQL: Otra Ambiguedad
• Sintaxis:
SELECT <alias1>.<attr1>, <alias2>.<attr2>
FROM <relation1> <alias1>, <relation2> <alias 2>
WHERE <alias1>.<A> = <alias2>.A;
• Sintaxis:
(SELECT-command)
• Usos
• Sintaxis:
SELECT …
FROM …
WHERE … (SELECT …
FROM … Consulta anidada
WHERE …)
Tipos de consulta anidada
• Tipos de consulta anidada:
• Sintaxis:
SELECT …
FROM R1
WHERE attr1 IN (SELECT attr2
FROM R2
WHERE R2.attr3 = R1.attr4);
Ejemplo: Consulta anidada correlacionada
• Sintaxis:
SELECT <attribute list>
FROM <table list>
WHERE <condition on the tables>
GROUP BY <grouping attributes>
HAVING <group condition>
Consulta SQL: Detalles del HAVING
• Consulta multi-tabla
• Join
• Consultas anidadas