-----ordenar desc
select*from Territories
order by TerritoryDescription desc
---ordenar asc
select*from Territories
order by TerritoryDescription asc
----- Selecionar todos los codigos postales que empiezan con 9
select* from Employees
where PostalCode like'9%'
order by PostalCode asc
--------selecciona al empleado con id 1
select*from Employees
where EmployeeID like '1'
--------selecciona 2 tablas
select*from Employees
select*from EmployeeTerritories
order by TerritoryID desc
Agregar el campo territoryid a la tabla employees
ALTER TABLE Employees add TerritoryID int;
UPDATE Employees
SET Employees.TerritoryID = EmployeeTerritories.TerritoryID
FROM Employees
INNER JOIN EmployeeTerritories ON Employees.EmployeeID =
EmployeeTerritories.EmployeeID;
--agregar el campo cuando es texto
alter table Territories add RegionDescription nvarchar(300)
UPDATE Territories
set Territories.RegionDescription=Region.RegionDescription
from Territories
inner join Region on Territories.RegionID=Region.RegionID
select *from Territories
---Agregar 2 campos
----añadir TerritoryID y TerritoryDescription
alter table Region add TerritoryID int, TerritoryDescription nvarchar(300)
select *from Region
UPDATE Region
SET Region.TerritoryID=Territories.TerritoryID,
Region.TerritoryDescription=Territories.TerritoryDescription
FROM Region
inner join Territories on Territories.RegionID=Region.RegionID
select *from Region
------Agregar el campo order ID a la tabla customers UPDATE SET
alter table customers add OrderID INT;
UPDATE Customers
SET Customers.OrderID=Orders.OrderID
FROM Customers
INNER JOIN Orders ON Customers.CustomerID=Orders.CustomerID
----Cruzar tablas y actualizar
select*from Categories
select*from Products
--------
alter table categories add ProductID int --- aumentar un campo
select*from Categories
select*from Products
update Categories -- actualizar la tabla
set categories.ProductID=Products.ProductID --aqui hago un cruce con el campo que
quiero
from Categories-- la misma tabla
inner join Products on Categories.CategoryID=Products.CategoryID--agarro la tabla
2 y el usuario clave
----contabiliza el total de productos por categoria
alter table categories add TotalProducts int
UPDATE Categories
SET Categories.ProductID = Products.ProductID,
Categories.TotalProducts = (
SELECT COUNT(*)
FROM Products
WHERE Products.CategoryID = Categories.CategoryID
)
FROM Categories
INNER JOIN Products ON Categories.CategoryID = Products.CategoryID;
Select*from Categories
---INNER JOIN
Select [Order Details].*, Orders.*
from [Order Details]
inner join Orders on [Order Details].OrderID=Orders.OrderID
select* from [Order Details]
select * from Orders
LEFT JOIN Orders ON [Order Details].OrderID = Orders.OrderID;
---GROUP BY
select* from [Order Details]
Select OrderID , SUM(UnitPrice) AS Total_Price
from [Order Details]
Group by OrderID
---GROUP BY
select*from [Order Details]
select OrderID, COUNT (*) AS Total_Cant
from [Order Details]
Group by OrderID
----CASE WHEN
select
OrderID,
ProductID,
UnitPrice,
Quantity,
Discount,
Case when UnitPrice > 20 then 'si' else 'no' end as bonus
from
[Order Details]
---
SELECT
OrderID,
ProductID,
UnitPrice,
Quantity,
Discount,
CASE
WHEN UnitPrice > 20 AND ProductID IN (
SELECT ProductID
FROM [Order Details]
GROUP BY ProductID
HAVING COUNT(*) > 4
) THEN 'si'
ELSE 'no'
END AS bonus
FROM
[Order Details]
select
EmployeeID,
LASTNAME,
FIRSTNAME,
TITLE,
CASE WHEN TITLE LIKE 'Sales Manager' THEN 'SI' ELSE 'NO' END AS GERENTE
FROM Employees