SQL - Basics II Flashcards
More advanced features in SQL.
The ORDER BY clause is used to sort the rows. What is the syntax for this?
SELECT column_name(s) FROM table_name ORDER BY column_name(s)
What keyword would you use if you want to sort values in descending order?
DESC
What keyword would you use if you want to sort values in ascending order?
ASC
AND and OR join two or more conditions in a WHERE clause.
The AND operator displays a row if ___ conditions listed are true.
all
AND and OR join two or more conditions in a WHERE clause.
The OR operator displays a row if ___ of the conditions listed are true.
any
AND (and OR - see hint) join two or more conditions in a WHERE clause.
What is the syntax of these statements?
SELECT column_name FROM table_name
WHERE columname’value’
AND columname’value’
The IN operator may be used if you know the exact value you want to return for at least one of the columns. What is the syntax for this expression?
SELECT column_name FROM table_name
WHERE column_name IN (value1,value2,..)
The BETWEEN … AND operator selects a range of data between two values. These values can be numbers, text, or dates. What is the syntax for this expression?
SELECT column_name FROM table_name
WHERE column_name
BETWEEN value1 AND value2
To display the persons outside the range used in a BETWEEN … AND statement, use the ___ operator.
NOT
With SQL, aliases can be used for column names and table names. What is the syntax for Column Name Alias?
SELECT column_name AS column_alias FROM table_name
With SQL, aliases can be used for column names and table names. What is the syntax for Table Name Alias?
SELECT column_name FROM table_name AS table_alias
Tables in a database can be related to each other with keys. A primary key is a column with a ______ _____ for each row.
unique value
We can select data from two tables by referring to two tables. If we had one table named Employees which had columns named Employee_ID(primary key) and Name and another table called Orders that had columns named Prod_ID(primary key), Product, and Employee_ID how would we find out who has ordered what product?
SELECT Employees.Name, Orders.Product
FROM Employees, Orders
WHERE Employees.Employee_ID=Orders.Employee_ID
We can select data from two tables by referring to two tables. If we had one table named Employees which had columns named Employee_ID(primary key) and Name and another table called Orders that had columns named Prod_ID(primary key), Product, and Employee_ID how would we find out who ordered a printer?
SELECT Employees.Name
FROM Employees, Orders
WHERE Employees.Employee_ID=Orders.Employee_ID
AND Orders.Product=’Printer’
We can select data from two tables with the INNER JOIN keyword. The INNER JOIN returns all rows from both tables where there is a match. If there are rows in table_name1 that do not have matches in table_name2, those rows will not be listed. What is the syntax for this?
SELECT table_name1.column_name, table_name2.column_name
FROM table_name1
INNER JOIN table_name2
ON table_name1.primary_key1 = table_name2.primary_key2
We can select data from two tables with the LEFT JOIN keyword. The LEFT JOIN returns all the rows from table_name1, even if there are no matches in table_name2. If there are rows in table_name1 that do not have matches in table_name2, those rows also will be listed. What is the syntax for LEFT JOIN?
SELECT table_name1.column_name, table_name2.column_name
FROM table_name1
LEFT JOIN table_name2
ON table_name1.primary_key1 = table_name2.primary_key2
We can select data from two tables with the RIGHT JOIN keyword. The RIGHT JOIN returns all the rows from table_name2, even if there are no matches in table_name1. If there had been any rows in table_name2 that did not have matches in table_name1, those rows also would have been listed. What is the syntax for RIGHT JOIN?
SELECT table_name1.column_name, table_name2.column_name
FROM table_name1
RIGHT JOIN table_name2
ON table_name1.primary_key1 = table_name2.primary_key2
The UNION command is used to select related information from two tables, much like the JOIN command. However, when using the UNION command all selected columns need to be of the ____ data type.
same
The UNION command is used to select related information from two tables, much like the JOIN command. With UNION, only distinct values are selected. What is the syntax for UNION?
SQL Statement 1
UNION
SQL Statement 2