Module 1.2: SQL SELECT Statement

0.0(0)
studied byStudied by 2 people
0.0(0)
full-widthCall Kai
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
GameKnowt Play
Card Sorting

1/28

flashcard set

Earn XP

Description and Tags

Prelim Topic

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

29 Terms

1
New cards

SELECT statement

  • It retrieves information from the database. With this, you can use Projection, Selection, and Joining.

2
New cards

Projection

  • Choose the columns in a table that are returned by a query. Choose as few or as many of the columns as needed.

3
New cards

Selection

  • Choose the rows in a table that are returned by a query. Various criteria can be used to restrict the rows that are retrieved.

4
New cards

Joining

  • Bring together data that is stored in different tables by specifying the link between them.

5
New cards

SELECT

(Syntax)

  • Is a list of one or more columns

6
New cards

*

(asterisk)

(Syntax)

  • This selects all columns

7
New cards

DISTINCT

(Syntax)

  • Suppresses duplicates

8
New cards

AS

(syntax)

  • keyword in SQL to give a column a temporary name, or alias, which serves as the heading in your query's result set. 

9
New cards

FROM

(syntax)

  • specifies the table containing the columns

10
New cards

Keyword

(SQL Terms)

  • It refers to an individual SQL statement

  • Ex: SELECT, FROM

11
New cards

Clause

(SQL Terms)

  • It is a part of a SQL statement

  • Ex: SELECT employee_id, last_name

12
New cards

Statement

(SQL Terms)

  • A combination of two or more clauses.

  • Ex: SELECT * FROM employees

13
New cards

SELECT * FROM departments;

This statement selects all columns of data in the “departments” table.

<p>This statement selects all columns of data in the “departments” table.</p>
14
New cards

SELECT department_id, location_id FROM departments;

  • This statement is used to display specific columns of the “departments” table by specifying column names, separated by commas.

  • This displays the columns department_id and location_id from the departments table

<ul><li><p>This statement is used to display specific columns of the “departments” table by specifying column names, separated by commas.</p></li><li><p>This displays the columns department_id and location_id from the departments table</p></li></ul><p></p>
15
New cards

SELECT last_name, salary, salary + 300 FROM employees;

  • This statement uses the addition operator to calculate a salary increase of 300 for all employees and displays a new SALARY + 300 column in the output.

  • Displays the “last_name” column, “salary” column, and the salary increase of 300 from the employees table

<ul><li><p>This statement uses the addition operator to calculate a salary increase of 300 for all employees and displays a new SALARY + 300 column in the output.</p></li><li><p>Displays the “last_name” column, “salary” column, and the salary increase of 300 from the employees table</p></li></ul><p></p>
16
New cards

SELECT last_name, salary, 12*(SALARY+100) FROM employees;

  • You can override rules of precedence by using parenthesis to specify the desired order in which operators are to be executed

  • This displays the last name, salary, and annual compensation of employees. It calculates the annual compensation as follows: adding a monthly bonus of $100 to the monthly salary, and then multiplying that subtotal by 12.

  • Because of the parenthesis, addition takes priority over multiplication

<ul><li><p>You can override rules of precedence by using parenthesis to specify the desired order in which operators are to be executed</p></li><li><p>This displays the last name, salary, and annual compensation of employees. It calculates the annual compensation as follows: adding a monthly bonus of $100 to the monthly salary, and then multiplying that subtotal by 12.</p></li><li><p>Because of the parenthesis, addition takes priority over multiplication</p></li></ul><p></p>
17
New cards

NULL

  • It is a value that is unavailable, unassigned, unknown, or inapplicable.

  • In a database, you can use this to represent unknown values in your database

18
New cards

True

a NULL value is not the same as a zero or a space

19
New cards

True

if you try to divide by NULL, the result is NULL or unknown

20
New cards

True

If you try to divide by zero, you get an error.

21
New cards

SELECT last_name AS name, commission_pct comm FROM employees;

(Using Column Aliases)

  • This displays the names and commission percentages of all the employees.

  • Notice that the optional AS keyword has been used before the column alias name.

  • The result of the query is the same whether the AS keyword is used or not.

<p>(Using Column Aliases)</p><ul><li><p>This displays the names and commission percentages of all the employees.</p></li></ul><ul><li><p>Notice that the optional AS keyword has been used before the column alias name.</p></li><li><p>The result of the query is the same whether the AS keyword is used or not.</p></li></ul><p></p>
22
New cards

SELECT last_name “Name”, salary*12 “Annual Salary” FROM employees;

(Using Column Aliases)

  • This displays the last names and annual salaries of all the employees.

  • Because Annual Salary contains a space, it has been enclosed in double quotation marks. Notice that the column heading in the output is exactly the same as the column alias

<p>(Using Column Aliases)</p><ul><li><p>This displays the last names and annual salaries of all the employees.</p></li><li><p>Because Annual Salary contains a space, it has been enclosed in double quotation marks. Notice that the column heading in the output is exactly the same as the column alias</p></li></ul><p></p>
23
New cards

True

By default, alias headings appear in uppercase

24
New cards

True

If an alias contains spaces or special characters like # or $, or if it is case sensitive, it is enclosed in double quotation marks (“ “)

25
New cards

True

It is best practice to always use the AS keyword when making an alias for a column to make the statement easier to read

26
New cards

||

  • The concatenation operator is represented by this

  • Links columns or character strings to other columns

  • creates a resultant column that is a character expression

27
New cards

SELECT last_name||job_id AS “Employees” FROM employees;

(Using Concatenation Operator)

  • In this example, LAST_NAME and JOB_ID are concatenated, and they are given the alias “Employees”

  • Notice that the employee last name and job code are combined to make single output column.

  • The AS keyword before the alias name makes the SELECT clause easier to read

<p>(Using Concatenation Operator)</p><ul><li><p>In this example, LAST_NAME and JOB_ID are concatenated, and they are given the alias “Employees”</p></li><li><p>Notice that the employee last name and job code are combined to make single output column.</p></li><li><p>The AS keyword before the alias name makes the SELECT clause easier to read</p></li></ul><p></p>
28
New cards

SELECT last_name || ‘is a’ || job_id AS “Employee Details” FROM employees;

(Using Concatenation Operator - Literal Character Strings)

  • This example displays last names and job codes of all employees. The column has the heading “Employee Details”

  • Notice the spaces between the single quotation marks in the SELECT statement. The spaces improve the readability of the output.

  • ‘is a’ is an example of a Literal Character Strings and is used as a concatenation to give the returned rows meaning.

<p>(Using Concatenation Operator - Literal Character Strings)</p><ul><li><p>This example displays last names and job codes of all employees. The column has the heading “Employee Details”</p></li><li><p>Notice the spaces between the single quotation marks in the SELECT statement. The spaces improve the readability of the output.</p></li><li><p>‘is a’ is an example of a Literal Character Strings and is used as a concatenation to give the returned rows meaning.</p></li></ul><p></p>
29
New cards

SELECT department name || q’ [, it’s assigned Manager Id: ]’ || manager_id AS “Department and Manager” FROM departments;

(Using Concatenation Operator - Alternative Quote (q) Operator)

  • In the example shown, the string contains a single quotation mark, which is normally interpreted as a delimiter of a character string. By using the q operator, however, the brackets [] are used as the quotation mark delimiter. The string between the brackets delimiters is interpreted as a literal character string.

<p>(Using Concatenation Operator - Alternative Quote (q) Operator)</p><ul><li><p><span><span>In the example shown, the string contains a single quotation mark, which is normally interpreted as a delimiter of a character string. By using the q operator, however, the </span></span><strong>brackets []</strong><span><span> are used as the quotation mark delimiter. The string between the brackets delimiters is interpreted as a literal character string.</span></span></p></li></ul><p></p>