QUERY practice Flashcards
Insert the missing statement to get all the columns from the Customers table.
________ * FROM Customers;
SELECT * FROM Customers
Write a statement that will select the City column from the Customers table.
______ _____ _____ customers;
SELECT City FROM Customers;
Select all the different values from the Country column in the Customers table.
____ _____ country FROM customers
SELECT DISTINCT country FROM customers
Select all records where the City column has the value “Berlin”.
SELECT * FROM customers ____ ___ = ______ ;
SELECT * FROM customers WHERE city = ‘Berlin’
Use the NOT keyword to select all records where City is NOT “Berlin”.
SELECT * FROM customers _________ = _______
SELECT * FROM customers WHERE NOT city = ‘Berlin’;
Select all records where the CustomerID column has the value 32.
SELECT * FROM customers ______ customer id __ __;
SELECT * FROM customers WHERE customerID = 32;
Select all records where the City column has the value ‘Berlin’ and the PostalCode column has the value 12209.
______ * FROM customers ___ city = ‘Berlin’ __ ____ = 12209
SELECT * FROM customers WHERE city = ‘Berlin’ AND postalcode = 12209
Select all records where the City column has the value ‘Berlin’ or ‘London’.
______ * FROM customers ___ city = ‘Berlin’ __ ___ = _______
SELECT * FROM customers WHERE City = ‘Berlin’ OR city = ‘London’
Select all records from the Customers table, sort the result alphabetically by the column City.
SELECT * FROM customers ______ _____
SELECT * FROM customers ORDER BY city;
Select all records from the Customers table, sort the result reversed alphabetically by the column City.
SELECT * FROM customers ____ ___ ___ ;
SELECT * FROM customers ORDER BY city DESC
Select all records from the Customers table, sort the result alphabetically, first by the column Country, then, by the column City.
SELECT * FROM customers _____ ___ ____;
SELECT * FROM customers ORDER BY country, city;
Insert a new record in the Customers table.
_____ Customers __ customername, address, city, postalcode, country__
_____ __ ‘Hekkan Burger’, ‘Gatevein 15’, ‘Sandnes’, ‘4306’, ‘Norway’__;
INSERT INTO Customers (customername, address, city, postalcode, country)
VALUES (‘Hekkan Burger’, ‘Gatevein 15’, ‘Sandnes’, ‘4306’, ‘Norway’);
Select all records from the Customers where the PostalCode column is empty.
SELECT * FROM customers WHERE ____ __ ____;
SELECT * FROM customers WHERE postalcode IS NULL;
Select all records from the Customers where the PostalCode column is NOT empty.
SELECT * FROM customers WHERE ___ ___ ___ ____;
SELECT * FROM customers WHERE postalcode IS NOT NULL
Update the City column of all records in the Customers table.
________ customers ___ city = ‘Oslo’
UPDATE customers SET city = ‘Oslo’;
Set the value of the City columns to ‘Oslo’, but only the ones where the Country column has the value “Norway”.
_____ customers __ city = ‘oslo’ ____ country = ‘Norway’
UPDATE customers SET city = ‘Oslo’ WHERE country = ‘Norway’
Update the City value and the Country value.
_____ customers __ city = ‘Oslo’ __ ______ = ‘Norway’ WHERE customerID = 32
UPDATE customers SET city = ‘Oslo’, country = ‘Norway’ WHERE customerID = 32
Delete all the records from the Customers table where the Country value is ‘Norway’.
_______ ______ customers _____ country =’Norway’
DELETE FROM customers WHERE country = ‘Norway’
Delete all the records from the Customers table.
________ customers;
DELETE FROM customers
Use the MIN function to select the record with the smallest value of the Price column.
SELECT ______ FROM products
SELECT MIN(price) FROM products
Use an SQL function to select the record with the highest value of the Price column.
SELECT________ FROM products
SELECT MAX(price) FROM products
Use the correct function to return the number of records that have the Price value set to 18.
SELECT ____ (*) FROM products ____ price = 18
SELECT COUNT(*) FROM products WHERE price = 18
Use an SQL function to calculate the average price of all products.
SELECT ________ FROM products
SELECT AVG(price) FROM products
Use an SQL function to calculate the sum of all the Price column values in the Products table.
SELECT _____ FROM products
SELECT SUM(price) FROM products
Select all records where the value of the City column starts with the letter “a”.
SELECT * FROM customers _______ ________
SELECT * FROM customers WHERE city LIKE ‘a%’
Select all records where the value of the City column ends with the letter “a”.
SELECT * FROM customers ____ ___ ___ ___
SELECT * FROM customers WHERE city LIKE ‘%a’
Select all records where the value of the City column contains the letter “a”.
SELECT * FROM customers ___ _____ ____ __
SELECT * FROM customers WHERE city LIKE ‘%a%’
Select all records where the value of the City column starts with letter “a” and ends with the letter “b”.
SELECT * FROM customers ___ _____ ____ __
SELECT * FROM customers WHERE city LIKE ‘a%b’
Select all records where the value of the City column does NOT start with the letter “a”.
SELECT * FROM customers ___ _____ ____ ___ __
SELECT * FROM customers WHERE city NOT LIKE ‘a%’
Select all records where the second letter of the City is an “a”.
SELECT * FROM customers WHERE city LIKE ‘_%’
SELECT * FROM customers WHERE city LIKE ‘_a%’
Select all records where the first letter of the City is an “a” or a “c” or an “s”.
SELECT * FROM customers WHERE city LIKE ‘___%’
SELECT * FROM customers WHERE city LIKE ‘[acs]%’
Select all records where the first letter of the City starts with anything from an “a” to an “f”.
SELECT * FROM customers WHERE city LIKE ‘____%’
SELECT * FROM customers WHERE city LIKE ‘[a-f]%’
Select all records where the first letter of the City is NOT an “a” or a “c” or an “f”.
SELECT * FROM customers WHERE city LIKE ‘____%’
SELECT * FROM customers WHERE city LIKE ‘[^acf]%’
Use the IN operator to select all the records where Country is either “Norway” or “France”.
SELECT * FROM customers _____ _____ ‘France’ _
SELECT * FROM customers WHERE country IN (‘Norway’, ‘France’)
Use the IN operator to select all the records where Country is NOT “Norway” and NOT “France”.
SELECT * FROM customers _____ ___ __ (‘Norway’, ‘France’)
SELECT * FROM customers WHERE country NOT IN (‘Norway’, ‘France’)
Use the BETWEEN operator to select all the records where the value of the Price column is between 10 and 20.
SELECT * FROM products WHERE price ______
SELECT * FROM products WHERE price BETWEEN 10 AND 20
Use the BETWEEN operator to select all the records where the value of the Price column is NOT between 10 and 20.
SELECT * FROM products WHERE price ______
SELECT * FROM products WHERE price NOT BETWEEN 10 AND 20
Use the BETWEEN operator to select all the records where the value of the ProductName column is alphabetically between ‘Geitost’ and ‘Pavlova’.
SELECT * FROM products WHERE productname _________
SELECT * FROM products WHERE productname BETWEEN ‘Geitost’ AND ‘Pavlova’
When displaying the Customers table, make an ALIAS of the PostalCode column, the column should be called Pno instead.
SELECT customername, address, postalcode ____ FROM customers
SELECT customername, address, postalcode AS pno FROM customers
When displaying the Customers table, refer to the table as Consumers instead of Customers.
SELECT * FROM customers ________
SELECT * FROM customers AS Consumers
Insert the missing parts in the JOIN clause to join the two tables Orders and Customers, using the CustomerID field in both tables as the relationship between the two tables.
SELECT * FROM orders
LEFT JOIN Customers _________ = ___________
SELECT * FROM orders
LEFT JOIN Customers
ON orders.customerid=customers.customerID
Choose the correct JOIN clause to select all records from the two tables where there is a match in both tables.
SELECT * FROM orders
________________ ON orders.customerID=customers.customerID
SELECT * FROM orders
INNER JOIN Customers
ON orders.customerid=customers.customerID
Choose the correct JOIN clause to select all the records from the Customers table plus all the matches in the Orders table.
SELECT * FROM orders
________________ ON
orders.customerID=customers.customerID
SELECT * FROM orders
RIGHT JOIN Customers
ON orders.customerid=customers.customerID
List the number of customers in each country.
SELECT ____(customerID), country FROM customers _________
SELECT COUNT(customerID), Country FROM customers GROUP BY country
List the number of customers in each country, ordered by the country with the most customers first.
SELECT \_\_\_\_\_\_\_ (CustomerID), country FROM customers \_\_\_\_\_\_\_\_\_\_ ORDER BY \_\_\_\_\_\_\_\_\_\_\_\_\_
SELECT COUNT(customerID), Country FROM customers GROUP BY country ORDER BY COUNT(CustomerID) DESC
Write the correct SQL statement to create a new database called testDB.
CREATE DATABASE testdb
Write the correct SQL statement to delete a database named testDB.
DROP DATABASE testDB
Write the correct SQL statement to create a new table called Persons.
\_\_\_\_\_\_\_\_\_\_\_\_\_ ( PersonID int, LastName varchar(255), FirstName varchar(255), Address varchar(255), City varchar(255) );
CREATE TABLE persons ( PersonID int, LastName varchar(255), FirstName varchar(255), Address varchar(255), City varchar(255) )
Write the correct SQL statement to delete a table called Persons.
DROP TABLE persons
Use the TRUNCATE statement to delete all data inside a table.
TRUNCATE TABLE persons
Add a column of type DATE called Birthday.
ALTER TABLE persons
ADD birthday DATE
Delete the column Birthday from the Persons table.
ALTER TABLE persons
DROP COLUMN birthday