Examen LBD2

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 7

The following SQL statement selects all customers with a City starting with "ber":

SELECT * FROM Customers WHERE City LIKE 'ber%';


The following SQL statement selects all customers with a City containing the pattern "es":

SELECT * FROM Customers WHERE City LIKE '%es%';


The following SQL statement selects all customers with a City ending with the letter "s":

SELECT * FROM Customers WHERE City LIKE '%s';


The following SQL statement selects all products with a price BETWEEN 10 and 20:

SELECT * FROM Products WHERE Price BETWEEN 10 AND 20;


The following SQL statement selects all orders with an OrderDate BETWEEN '04-July-1996' and '09-July-1996':

SELECT * FROM Orders WHERE OrderDate BETWEEN #07/04/1996# AND #07/09/1996#;


The following SQL statement selects all the orders from the customer with CustomerID=4 (Around the Horn). We use the "Customers" and "Orders" tables, and give them the table aliases of "c" and "o" respectively (Here we have used aliases to make the SQL shorter):

SELECT o.OrderID, o.OrderDate, c.CustomerName FROM Customer c, Orders o WHERE c.CustomerName="Around the Horn" AND

c.CustomerID=o.CustomerID;
INNER JOIN is the same as JOIN.

SELECT Customers.CustomerName, Orders.OrderID FROM Customers INNER JOIN Orders ON Customers.CustomerID=Orders.CustomerID ORDER BY Customers.CustomerName;
Note: The LEFT JOIN keyword returns all the rows from the left table (Customers), even if there are no matches in the right table (Orders). The following SQL statement will return all employees, and any orders they have placed:

SELECT Orders.OrderID, Employees.FirstName FROM Orders RIGHT JOIN Employees ON Orders.EmployeeID=Employees.EmployeeID ORDER BY Orders.OrderID;
Note: The RIGHT JOIN keyword returns all the rows from the right table (Employees), even if there are no matches in the left table (Orders). The following SQL statement creates a database called "my_db": CREATE DATABASE my_db; The NOT NULL constraint enforces a column to NOT accept NULL values.

CREATE TABLE PersonsNotNull ( P_Id int NOT NULL, LastName varchar(255) NOT NULL, FirstName varchar(255), Address varchar(255),

City varchar(255) )
The following SQL creates a UNIQUE constraint on the "P_Id" column when the "Persons" table is created: CREATE TABLE PersonsUnique ( P_Id int NOT NULL UNIQUE, LastName varchar(255) NOT NULL, FirstName varchar(255), Address varchar(255), City varchar(255) ) The following SQL creates a UNIQUE constraint on the "P_Id" column when the "Persons" table is created: CREATE TABLE PersonsUnique ( P_Id int NOT NULL UNIQUE, LastName varchar(255) NOT NULL, FirstName varchar(255), Address varchar(255), City varchar(255) ) The following SQL creates a CHECK constraint on the "P_Id" column when the "Persons" table is created. The CHECK constraint specifies that the column "P_Id" must only include integers greater than 0. CREATE TABLE Persons ( P_Id int NOT NULL, LastName varchar(255) NOT NULL, FirstName varchar(255), Address varchar(255), City varchar(255), CHECK (P_Id>0) )

The SQL statement below creates an index named "PIndex" on the "LastName" column in the "Persons" table: CREATE INDEX PIndex ON Persons (LastName) CREATE INDEX PIndex ON Persons (LastName, FirstName) What if we only want to delete the data inside the table, and not the table itself? Then, use the TRUNCATE TABLE statement: TRUNCATE TABLE table_name The ALTER TABLE statement is used to add, delete, or modify columns in an existing table. Now we want to add a column named "DateOfBirth" in the "Persons" table. ALTER TABLE Persons ADD DateOfBirth date Now we want to change the data type of the column named "DateOfBirth" in the "Persons" table. ALTER TABLE Persons ALTER COLUMN DateOfBirth year Next, we want to delete the column named "DateOfBirth" in the "Persons" table. ALTER TABLE Persons DROP COLUMN DateOfBirth

The COUNT() function returns the number of rows that matches a specified criteria.
The following SQL statement counts the number of orders from "CustomerID"=7 from the "Orders" table:

SELECT COUNT(CustomerID) AS OrdersFromCustomerID7 FROM Orders WHERE CustomerID=7;


The following SQL statement counts the total number of orders in the "Orders" table:

SELECT COUNT(*) AS NumberOfOrders FROM Orders;


The following SQL statement counts the number of unique customers in the "Orders" table:

SELECT COUNT(DISTINCT CustomerID) AS NumberOfCustomers FROM Orders;


The LAST() function returns the last value of the selected column. SELECT CustomerName FROM Customers ORDER BY CustomerID DESC LIMIT 1; The MAX() function returns the largest value of the selected column. The following SQL statement gets the largest value of the "Price" column from the "Products" table:

SELECT MAX(Price) AS HighestPrice FROM Products;


The SUM() function returns the total sum of a numeric column. The following SQL statement finds the sum of all the "Quantity"

fields for the "OrderDetails" table:

SELECT SUM(Quantity) AS TotalItemsOrdered FROM OrderDetails;


The GROUP BY statement is used in conjunction with the aggregate functions to group the result-set by one or more columns. The following SQL statement counts as orders grouped by shippers:

SELECT Shippers.ShipperName,COUNT(Orders.OrderID) AS NumberOfOrders FROM Orders LEFT JOIN Shippers ON Orders.ShipperID=Shippers.ShipperID GROUP BY ShipperName;
We can also use the GROUP BY statement on more than one column, like this:

SELECT Shippers.ShipperName, Employees.LastName, COUNT(Orders.OrderID) AS NumberOfOrders FROM ((Orders INNER JOIN Shippers ON Orders.ShipperID=Shippers.ShipperID) INNER JOIN Employees ON Orders.EmployeeID=Employees.EmployeeID) GROUP BY ShipperName,LastName;
The HAVING clause was added to SQL because the WHERE keyword could not be used with aggregate functions. The following SQL statement finds if any of the employees has registered more than 10 orders:

SELECT Employees.LastName, COUNT(Orders.OrderID) AS NumberOfOrders FROM (Orders INNER JOIN Employees ON Orders.EmployeeID=Employees.EmployeeID) GROUP BY LastName HAVING COUNT(Orders.OrderID) > 10;
select c.ciudad, sum(a.total),count(a.folio) from factura a, clientes b, ciudad c where a.id_cliente=b.id and b.id_ciudad=c.id group by a.id_cliente having sum(a.total)>3000000 limit 1;

You might also like