SQL commands Flashcards
The following SQL statement selects all the records in the “Customers” table:
SELECT * FROM Customers;
The following SQL statement selects the “CustomerName” and “City” columns from the “Customers” table:
SELECT CustomerName,City FROM Customers;
The following SQL statement selects only the distinct values from the “City” columns from the “Customers” table:
SELECT DISTINCT City FROM Customers;
The following SQL statement selects all the customers from the country “Mexico”, in the “Customers” table:
SELECT * FROM Customers WHERE Country=’Mexico’;
The following SQL statement selects all the customers whose CustomerID is equal to 1, in the “Customers” table:
SELECT * FROM Customers WHERE CustomerID=1;
The following SQL statement selects all customers from the country “Germany” AND the city “Berlin”, in the “Customers” table:
SELECT * FROM Customers WHERE Country=’Germany’ AND City=’Berlin’;
The following SQL statement selects all customers from the city “Berlin” OR “München”, in the “Customers” table:
SELECT * FROM Customers WHERE City=’Berlin’ OR City=’München’;
The following SQL statement selects all customers from the country “Germany” AND the city must be equal to “Berlin” OR “München”, in the “Customers” table:
SELECT * FROM Customers WHERE Country=’Germany’ AND (City=’Berlin’ OR City=’München’);
The following SQL statement selects all customers from the “Customers” table, sorted by the “Country” column:
SELECT * FROM Customers ORDER BY Country;
The following SQL statement selects all customers from the “Customers” table, sorted DESCENDING by the “Country” column:
SELECT * FROM Customers ORDER BY Country DESC;
The following SQL statement selects all customers from the “Customers” table, sorted by the “Country” and the “CustomerName” column:
SELECT * FROM Customers ORDER BY Country,CustomerName;
The following SQL statement will insert a new row, but only insert data in the “CustomerName”, “City”, and “Country” columns, with ‘Cardinal’, ‘Stavanger’, and ‘Norway’, respectively.
INSERT INTO Customers (CustomerName, City, Country) VALUES (‘Cardinal’, ‘Stavanger’, ‘Norway’);
The following SQL statement will update the customer “Alfreds Futterkiste” with the ContactName ‘Alfred Schmidt’ and the City ‘Hamburg’.
UPDATE Customers SET ContactName=’Alfred Schmidt’, City=’Hamburg’ WHERE CustomerName=’Alfreds Futterkiste’;
Assume we wish to delete the customer “Alfreds Futterkiste” from the “Customers” table.
DELETE FROM Customers WHERE CustomerName=’Alfreds Futterkiste’;
The following SQL statement will delete all the rows from Customers [2]
- DELETE FROM Customers; 2. DELETE * FROM Customers;
The following SQL statement selects the two first records from the “Customers” table:
SELECT TOP 2 * FROM Customers;
The following SQL statement selects the first 50% of the records from the “Customers” table:
SELECT TOP 50 PERCENT * FROM Customers;
The following SQL statement selects all customers with a City starting with the letter “s”:
SELECT * FROM Customers WHERE City LIKE ‘s%’;
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 customers with a Country containing the pattern “land”:
SELECT * FROM Customers WHERE Country LIKE ‘%land%’;
The following SQL statement selects all customers with a Country NOT containing the pattern “land”:
SELECT * FROM Customers WHERE Country NOT LIKE ‘%land%’;
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 starting with any character, followed by “erlin”:
SELECT * FROM Customers WHERE City LIKE ‘_erlin’;
The following SQL statement selects all customers with a City starting with “L”, followed by any character, followed by “n”, followed by any character, followed by “on”:
SELECT * FROM Customers WHERE City LIKE ‘L_n_on’;
The following SQL statement selects all customers with a City starting with “b”, “s”, or “p”:
SELECT * FROM Customers WHERE City LIKE ‘[bsp]%’;
The following SQL statement selects all customers with a City starting with “a”, “b”, or “c”:
SELECT * FROM Customers WHERE City LIKE ‘[a-c]%’;
The following SQL statement selects all customers with a City NOT starting with “b”, “s”, or “p”:
SELECT * FROM Customers WHERE City LIKE ‘[!bsp]%’;
The following SQL statement selects all customers with a City of “Paris” or “London”:
SELECT * FROM Customers WHERE City IN (‘Paris’,’London’);
The following SQL statement selects all products with a price BETWEEN 10 and 20:
SELECT * FROM Products WHERE Price BETWEEN 10 AND 20;
To display the products outside the range of the previous example, use NOT BETWEEN:
SELECT * FROM Products WHERE Price NOT BETWEEN 10 AND 20;
The following SQL statement selects all products with a price BETWEEN 10 and 20, but products with a CategoryID of 1,2, or 3 should not be displayed:
SELECT * FROM Products WHERE (Price BETWEEN 10 AND 20) AND NOT CategoryID IN (1,2,3);
The following SQL statement selects all products with a ProductName beginning with any of the letter BETWEEN ‘C’ and ‘M’:
SELECT * FROM Products WHERE ProductName BETWEEN ‘C’ AND ‘M’;
The following SQL statement selects all products with a ProductName beginning with any of the letter NOT BETWEEN ‘C’ and ‘M’:
SELECT * FROM Products WHERE ProductName NOT BETWEEN ‘C’ AND ‘M’;
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 specifies two aliases, one for the CustomerName column and one for the ContactName column, “Customer” and “Contact Name”, respectively.
SELECT CustomerName AS Customer, ContactName AS [Contact Person] FROM Customers;
In the following SQL statement we combine four columns (Address, City, PostalCode, and Country) and create an alias named “Address”:
SELECT CustomerName, Address+’, ‘+City+’, ‘+PostalCode+’, ‘+Country AS Address FROM Customers;
The following SQL statement selects all the orders from the customer with CustomerName = “Around the Horn” with CustomerName column from the Customers table and OrderID and OrderDate from the Orders Table. We use the “Customers” and “Orders” tables, and give them the table aliases of “c” and “o” respectively.
SELECT o.OrderID, o.OrderDate, c.CustomerName FROM Customers AS c, Orders AS o WHERE c.CustomerName=”Around the Horn”