SQL - Basics II Flashcards

More advanced features in SQL.

1
Q

The ORDER BY clause is used to sort the rows. What is the syntax for this?

A
SELECT column_name(s) FROM table_name
ORDER BY column_name(s)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What keyword would you use if you want to sort values in descending order?

A

DESC

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What keyword would you use if you want to sort values in ascending order?

A

ASC

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

AND and OR join two or more conditions in a WHERE clause.

The AND operator displays a row if ___ conditions listed are true.

A

all

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

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.

A

any

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

AND (and OR - see hint) join two or more conditions in a WHERE clause.

What is the syntax of these statements?

A

SELECT column_name FROM table_name
WHERE columname’value’
AND columname’value’

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

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?

A

SELECT column_name FROM table_name

WHERE column_name IN (value1,value2,..)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

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?

A

SELECT column_name FROM table_name
WHERE column_name
BETWEEN value1 AND value2

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

To display the persons outside the range used in a BETWEEN … AND statement, use the ___ operator.

A

NOT

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

With SQL, aliases can be used for column names and table names. What is the syntax for Column Name Alias?

A

SELECT column_name AS column_alias FROM table_name

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

With SQL, aliases can be used for column names and table names. What is the syntax for Table Name Alias?

A

SELECT column_name FROM table_name AS table_alias

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Tables in a database can be related to each other with keys. A primary key is a column with a ______ _____ for each row.

A

unique value

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

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?

A

SELECT Employees.Name, Orders.Product
FROM Employees, Orders
WHERE Employees.Employee_ID=Orders.Employee_ID

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

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?

A

SELECT Employees.Name
FROM Employees, Orders
WHERE Employees.Employee_ID=Orders.Employee_ID
AND Orders.Product=’Printer’

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

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?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

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?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

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?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

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.

A

same

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

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?

A

SQL Statement 1
UNION
SQL Statement 2

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

The _____ ___ command is equal to the UNION command, except that it selects all values.

A

UNION ALL

21
Q

The UNION ALL command is equal to the UNION command, except that UNION ALL selects all values. What is the syntax for UNION ALL?

A

SQL Statement 1
UNION ALL
SQL Statement 2

22
Q

What is the syntax used to create a new database?

A

CREATE DATABASE database_name

23
Q

What is the syntax for creating a new table in a database?

A
CREATE TABLE table_name
(
column_name1 data_type,
column_name2 data_type,
.......
)
24
Q

In the example below, what does the numbers in parenthases indicate?

CREATE TABLE Person
(
LastName varchar(30),
FirstName varchar,
Address varchar,
Age int(3)
)
A

The maximum length for the column.

25
Q

The data type for creating a table with specifies what type of data the column can hold. What are the four integer values?

A

integer(size)
int(size)
smallint(size)
tinyint(size)

26
Q

The data type for creating a table with specifies what type of data the column can hold. What are the two most common data types for decimal numbers?

A

decimal(size,d)

numeric(size,d)

27
Q

The data type for creating a table with specifies what type of data the column can hold. What is the type used for a fixed length string (can contain letters, numbers, and special characters).

A

char(size)

28
Q

The data type for creating a table with specifies what type of data the column can hold. What is the data type for a variable length string (can contain letters, numbers, and special characters).

A

varchar(size)

29
Q

The data type for creating a table with specifies what type of data the column can hold. What is the data type used to hold a date?

A

date(yyyymmdd)

30
Q

You can create an _____ in an existing table to locate rows more quickly and efficiently. It is possible to create one on one or more columns of a table, and each is given a name. The users cannot see them, they are just used to speed up queries.

A

index

31
Q

A unique index means that two rows cannot have the same index value. What is the syntax for a unique index?

A
CREATE UNIQUE INDEX index_name
ON table_name (column_name)
32
Q

A simple index, one without the UNIQUE keyword, allows duplicate values. What is the syntax for a simple index?

A
CREATE INDEX index_name
ON table_name (column_name)
33
Q

You can delete an existing index in a table with the ____ _____ statement.

A

DROP INDEX

34
Q

You can delete an existing index in a table with the DROP INDEX statement.

What is the syntax for Microsoft SQLJet (and Microsoft Access)?

A

DROP INDEX index_name ON table_name

35
Q

You can delete an existing index in a table with the DROP INDEX statement.

What is the syntax for MS SQL Server?

A

DROP INDEX table_name.index_name

36
Q

You can delete an existing index in a table with the DROP INDEX statement.

What is the syntax for MySQL?

A

ALTER TABLE table_name DROP INDEX index_name

37
Q

To delete a table (the table structure, attributes, and indexes will also be deleted) you would use what syntax?

A

DROP TABLE table_name

38
Q

To delete a database what syntax would you use?

A

DROP DATABASE database_name

39
Q

If you only want to get rid of the data inside a table, and not the table itself, use the TRUNCATE TABLE command. What is the syntax for this command?

A

TRUNCATE TABLE table_name

40
Q

The _____ _____ statement is used to add or drop columns in an existing table.

A

ALTER TABLE

41
Q

The ALTER TABLE statement is used to add or drop columns in an existing table. What is the ADD command syntax?

A

ALTER TABLE table_name

ADD column_name datatype

42
Q

The ALTER TABLE statement is used to add or drop columns in an existing table. What is the DROP COLUMN command syntax?

A

ALTER TABLE table_name

DROP COLUMN column_name

43
Q

What is the syntax for built-in SQL functions?

A

SELECT function(column) FROM table

44
Q

What are two basic types of functions in SQL?

A

Aggregate Functions and Scalar functions

45
Q

GROUP BY… was added to SQL because aggregate functions (like SUM) return the aggregate of all column values every time they are called, and without the GROUP BY function it was impossible to find the sum for each individual group of column values. What is the syntax for this command?

A

SELECT column_name1,SUM(column_name2) FROM table GROUP BY column_name1

46
Q

HAVING… was added to SQL because the WHERE keyword could not be used against aggregate functions (like SUM), and without HAVING… it would be impossible to test for result conditions.

The syntax for the HAVING function is?

A

SELECT column,SUM(column) FROM table
GROUP BY column
HAVING SUM(column) condition value

47
Q

The SELECT INTO statement is most often used to create backup copies of tables or for archiving records. What is the syntax for this statement

A
SELECT column_name(s) INTO newtable [IN externaldatabase]
FROM source
48
Q

What is a view in SQL?

A

A VIEW is a virtual table based on the result-set of a SELECT statement.

49
Q

A view contains rows and columns, just like a real table. The fields in a view are fields from one or more real tables in the database. You can add SQL functions, WHERE, and JOIN statements to a view and present the data as if the data were coming from a single table. What is the syntax for a VIEW statement?

Note: The database design and structure will NOT be affected by the functions, where, or join statements in a view.

A

CREATE VIEW view_name AS
SELECT column_name(s)
FROM table_name
WHERE condition