SQL commands Flashcards
(95 cards)
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’;










