Free Data Warehousing on AWS: SQL Questions and Answers — Questions and Answers
Question 1: What exactly does SQL mean?
- Structured Question Language
- Strong Question Language
- Structured Query Language (Correct answer)
Correct answer: Structured Query Language
SQL stands for Structured Query Language, which is the standard language for managing and manipulating relational databases. It is used to perform tasks such as retrieving data, updating records, inserting new data, and deleting data. Understanding SQL is fundamental for anyone working with data warehousing and database systems.
Question 2: Which SQL command is employed to retrieve data from a database?
- GET
The correct SQL command to retrieve data from a database is `SELECT`. The `SELECT` statement is fundamental for querying databases, allowing users to specify which columns and rows they want to fetch from one or more tables. It is the most frequently used command for data retrieval operations.
Question 3: How does one update data in a database using SQL?
- SAVE
- UPDATE (Correct answer)
- SAVE AS
- MODIFY
Correct answer: UPDATE
The `UPDATE` SQL command is specifically used to modify existing records in a database table. It allows users to change the values of one or more columns for specific rows that meet a defined condition. This command is essential for maintaining the accuracy and currency of data within a database.
Question 4: How do you delete data from a database using SQL?
- COLLAPSE
- DELETE (Correct answer)
- REMOVE
Correct answer: DELETE
The `DELETE` SQL command is used to remove existing records from a database table. Users can specify conditions to delete particular rows, or if no condition is given, all rows from the table will be removed. This command is crucial for data management and maintaining relevant information in a database.
Question 5: Which SQL statement is employed when fresh data is to be added to a database?
- INSERT NEW
- ADD NEW
- INSERT INTO (Correct answer)
- ADD RECORD
Correct answer: INSERT INTO
The `INSERT INTO` SQL statement is the standard command used to add new rows or records into a database table. It allows users to specify the table name and the values for each column, thereby populating the database with new data. This command is fundamental for data entry and expanding a dataset.
Question 6: How do you use SQL to choose the "FirstName" column from the "Persons" table?
- EXTRACT FirstName FROM Persons
- SELECT Persons.FirstName
- SELECT FirstName FROM Persons (Correct answer)
Correct answer: SELECT FirstName FROM Persons
The `SELECT` statement is fundamental in SQL for retrieving data from a database. To specify a particular column, you list its name directly after `SELECT`. The `FROM` clause then indicates which table the column belongs to, making `SELECT FirstName FROM Persons` the correct syntax for this operation.
Question 7: How do you use SQL to select every column from the "Persons" table?
- SELECT Persons
- SELECT *.Persons
- SELECT [all] FROM Persons
- SELECT * FROM Persons (Correct answer)
Correct answer: SELECT * FROM Persons
In SQL, the asterisk (`*`) is a wildcard character used to represent all columns in a table. When placed after the `SELECT` keyword, it instructs the database to return every column. Therefore, `SELECT * FROM Persons` is the standard and correct way to retrieve all columns from the specified table.
Question 8: How do you use SQL to retrieve every record from the "Persons" table when "Peter" is the value of the "FirstName" column?
- SELECT * FROM Persons WHERE FirstName<>'Peter'
- SELECT [all] FROM Persons WHERE FirstName LIKE 'Peter'
- SELECT * FROM Persons WHERE FirstName='Peter' (Correct answer)
- SELECT [all] FROM Persons WHERE FirstName='Peter'
Correct answer: SELECT * FROM Persons WHERE FirstName='Peter'
To filter records based on a specific condition, SQL uses the `WHERE` clause. For an exact match on a string value, the equals sign (`=`) operator is used, with the string enclosed in single quotes. Thus, `SELECT * FROM Persons WHERE FirstName='Peter'` correctly retrieves all records where the 'FirstName' column is exactly 'Peter'.
Question 9: How do you use SQL to pick every record from the "Persons" table where the value of the "FirstName" column begins with a "a"?
- SELECT * FROM Persons WHERE FirstName LIKE 'a%' (Correct answer)
- SELECT * FROM Persons WHERE FirstName LIKE '%a'
- SELECT * FROM Persons WHERE FirstName='a'
- SELECT * FROM Persons WHERE FirstName='%a%'
Correct answer: SELECT * FROM Persons WHERE FirstName LIKE 'a%'
The `LIKE` operator in SQL is used for pattern matching, often in conjunction with wildcard characters. The percent sign (`%`) wildcard matches any sequence of zero or more characters. To find names beginning with 'a', `a%` is used, meaning 'a' followed by any characters, making `SELECT * FROM Persons WHERE FirstName LIKE 'a%'` the correct query.
Question 10: If ANY of the listed conditions are true, the OR operator displays a record. If ALL of the specified criteria are true, the AND operator displays a record.
- True (Correct answer)
- False
Correct answer: True
This statement accurately describes the behavior of the `OR` and `AND` logical operators in SQL. The `OR` operator returns true if at least one of its conditions is true, while the `AND` operator requires all specified conditions to be true for the entire expression to be true. This is a core concept in constructing complex `WHERE` clauses.
Question 11: How do you use SQL to retrieve every record where Peter is the first name and Jackson is the last name from the "Persons" table?
- SELECT * FROM Persons WHERE FirstName<>'Peter' AND LastName<>'Jackson'
- SELECT FirstName='Peter', LastName='Jackson' FROM Persons
- SELECT * FROM Persons WHERE FirstName='Peter' AND LastName='Jackson (Correct answer)
Correct answer: SELECT * FROM Persons WHERE FirstName='Peter' AND LastName='Jackson
To apply multiple conditions that must all be true for a record to be selected, the `AND` logical operator is used within the `WHERE` clause. Each condition is specified separately, connected by `AND`. Therefore, `SELECT * FROM Persons WHERE FirstName='Peter' AND LastName='Jackson'` correctly filters for records matching both criteria.
Question 12: How do you use SQL to retrieve every record from a table called "Persons" where the "LastName" is between (and includes) "Hansen" and "Pettersen" alphabetically?
- SELECT * FROM Persons WHERE LastName>'Hansen' AND LastName<'Pettersen'
- SELECT * FROM Persons WHERE LastName BETWEEN 'Hansen' AND 'Pettersen' (Correct answer)
- SELECT LastName>'Hansen' AND LastName<'Pettersen' FROM Persons
Correct answer: SELECT * FROM Persons WHERE LastName BETWEEN 'Hansen' AND 'Pettersen'
The `BETWEEN` operator in SQL is specifically designed to select values within a specified range, including the start and end values. It provides a concise way to filter records based on a range for numbers, dates, or, as in this case, alphabetical strings. `SELECT * FROM Persons WHERE LastName BETWEEN 'Hansen' AND 'Pettersen'` is the most appropriate and readable solution.
Question 13: Which SQL query is employed to only return distinct values?
- SELECT DISTINCT (Correct answer)
- SELECT UNIQUE
- SELECT DIFFERENT
Correct answer: SELECT DISTINCT
The `DISTINCT` keyword in SQL is used with the `SELECT` statement to eliminate duplicate rows from the result set. When you want to see only unique values for a column or combination of columns, `SELECT DISTINCT` is the standard and correct syntax. Options like `UNIQUE` or `DIFFERENT` are not standard SQL keywords for this purpose.
Question 14: Which SQL query is employed to only return distinct values?
- SORT BY
- ORDER BY (Correct answer)
- ORDER
- SORT
Correct answer: ORDER BY
The `ORDER BY` clause in SQL is used to sort the result set of a query. It allows you to arrange the data in ascending (ASC) or descending (DESC) order based on one or more columns. `ORDER BY` is the standard SQL keyword for sorting, making it the correct choice among the given options.
Question 15: How can you use SQL to return every record from a "Persons" table ordered by "FirstName"?
- SELECT * FROM Persons SORT 'FirstName' DESC
- SELECT * FROM Persons ORDER FirstName DESC
- SELECT * FROM Persons SORT BY 'FirstName' DESC
- SELECT * FROM Persons ORDER BY FirstName DESC (Correct answer)
Correct answer: SELECT * FROM Persons ORDER BY FirstName DESC
To sort query results, SQL uses the `ORDER BY` clause, followed by the column name(s) to sort by. The `DESC` keyword explicitly specifies descending order. Therefore, `SELECT * FROM Persons ORDER BY FirstName DESC` is the correct and standard SQL syntax for retrieving all records from 'Persons' and sorting them by 'FirstName' in descending order.
Question 16: How do you add a new record using SQL to the "Persons" table?
- INSERT VALUES ('Jimmy', 'Jackson') INTO Persons
- INSERT ('Jimmy', 'Jackson') INTO Persons
- INSERT INTO Persons VALUES ('Jimmy', 'Jackson') (Correct answer)
Correct answer: INSERT INTO Persons VALUES ('Jimmy', 'Jackson')
The `INSERT INTO` statement is used in SQL to add new rows (records) to a table. The basic syntax involves specifying the table name, followed by the `VALUES` keyword, and then the list of values for each column, enclosed in parentheses. `INSERT INTO Persons VALUES ('Jimmy', 'Jackson')` correctly adds a new record with the specified first and last names.
What exactly does SQL mean?