SQL Statements Explained

Choose a study mode

Play Quiz
Study Flashcards
Spaced Repetition
Chat to Lesson

Podcast

Play an AI-generated podcast conversation about this lesson
Download our mobile app to listen on the go
Get App

Questions and Answers

What is the effect of the following SQL code snippet?

  • Performs an undo operation on the delete operation.
  • Deletes a row from the customer table where id equals 1.
  • Both A and B (correct)
  • None of the above

Regarding performance, which SQL statement is generally faster?

  • UNION
  • Both have the same performance
  • UNION ALL (correct)
  • None

What is the output of the SQL query select * from employees where rownum = 5;?

  • No rows selected. (correct)
  • Prints the records of the 1st to 5th rows from the employee's table.
  • SQL query error.
  • Prints the record of the 5th row from the employee's table.

Which command is used to search for NULL values in SQL?

<p>IS NULL (D)</p> Signup and view all the answers

What is it called when multiple columns are collectively used as the Primary Key?

<p>Composite Key (B)</p> Signup and view all the answers

What will be the output of the following SQL command?

<p>Error at line 3 as group functions are not allowed here. (B)</p> Signup and view all the answers

If Select count(product_id) from order; returns 15, what is the maximum possible number of outputs for the query Select count(distinct product_id) from order;?

<p>&lt;= 15 (A)</p> Signup and view all the answers

Which SQL keyword is used to combine the results of two or more SELECT statements?

<p>UNION (C)</p> Signup and view all the answers

Which SQL JOIN type returns all records from the left table and matching records from the right table?

<p>LEFT JOIN (A)</p> Signup and view all the answers

Which SQL keyword is used to retrieve data from a database table?

<p>SELECT (A)</p> Signup and view all the answers

Which SQL statement is used to permanently save changes to the database?

<p>COMMIT (A)</p> Signup and view all the answers

In SQL, which operator is used to check for matching values in a specified list?

<p>IN (D)</p> Signup and view all the answers

Which of the following commands is equivalent to the query Select e.name, d.name from employees e join departments d on e.department_id = d.department_id;?

<p>Select e.name, d.name from employees e natural join departments d; (C)</p> Signup and view all the answers

Which of the following is a single-row function in SQL?

<p>Concat (A)</p> Signup and view all the answers

What will be the output of the following function? select substr('helloworld', -1, instr('helloworld', 'I', 3, 2)) from dual;

<p>d (D)</p> Signup and view all the answers

SQL Aggregate functions always return how many values?

<p>Single (D)</p> Signup and view all the answers

We can use a literal in the CONCAT function. What does literal refer to?

<p>All of the above (D)</p> Signup and view all the answers

Which symbol represents the concatenation operator in SQL?

<p>|| (C)</p> Signup and view all the answers

Which of the following queries finds the total number of employees working on 'HR' departments?

<p>select count(*) from employees where department_id = (Select id from departments where name = 'HR'); (C)</p> Signup and view all the answers

Which of the following queries finds the total rating of sailors who have reserved boat 103?

<p>SELECT SUM(s.rating) FROM sailors s, reserves r WHERE s.sid = r.sid AND r.bid = 103; (C)</p> Signup and view all the answers

Flashcards

Delete and Rollback

The code snippet attempts to delete a row from the 'customer' table where the 'id' is 1, and then rolls back, effectively undoing the deletion.

UNION vs. UNION ALL

UNION ALL is faster because it does not remove duplicate rows, unlike UNION.

SQL ROWNUM

The query will return no rows because rownum starts at 1, and the condition rownum = 5 can never be true during the initial rownum assignment.

Search for NULL values

IS NULL is the operator used to search for NULL values in SQL.

Signup and view all the flashcards

Composite Key

When multiple columns are collectively used as the Primary Key, it is a Composite Key.

Signup and view all the flashcards

Aggregate Function Error

This query will result in an error because you cannot use aggregate functions (AVG) in the WHERE clause.

Signup and view all the flashcards

Count Distinct

The query Select count(distinct product_id) from order; will give <= 15 outputs, as count(distinct product_id) counts the number of unique product IDs, which can be at most 15.

Signup and view all the flashcards

Combine SELECT statements

The UNION keyword is used to combine the results of two or more SELECT statements into a single result set.

Signup and view all the flashcards

LEFT JOIN

LEFT JOIN returns all records from the left table and matching records from the right table.

Signup and view all the flashcards

Retrieve data

The SELECT keyword is used to retrieve data from a database table.

Signup and view all the flashcards

Permanent Database changes

The COMMIT statement is used to make permanent changes to the database.

Signup and view all the flashcards

Check for Matching Values

The IN operator is used to check for matching values in a specified list.

Signup and view all the flashcards

Equivalent query

Select e.name, d.name from employees e natural join departments d;

Signup and view all the flashcards

Single-row function

Concat

Signup and view all the flashcards

Following Function Output

d

Signup and view all the flashcards

SQL Aggregate function return values

Single

Signup and view all the flashcards

Literal in concat

All of the above

Signup and view all the flashcards

What is the symbol of Concatenation Operator?

||

Signup and view all the flashcards

total number of employees working on 'HR' departments?

select count(*) from employees where department_id = (Select id from departments where name = 'HR');

Signup and view all the flashcards

total rating of the sailors who have reserved boat 103

SELECT SUM(s.rating) FROM sailors s, reserves r WHERE s.sid = r.sid AND r.bid = 103;

Signup and view all the flashcards

Study Notes

  • DELETE FROM customer WHERE id = 1; ROLLBACK; performs a delete operation and then attempts to undo it.

  • The code snippet deletes a row from the customer table where the id is 1, then attempts to rollback the operation.

  • UNION ALL is faster than UNION in terms of performance.

  • SELECT * FROM employees WHERE rownum = 5; will yield no rows selected.

  • The command to search for NULL values is IS NULL.

  • When multiple columns collectively act as the Primary Key, it is referred to as a Composite Key.

SQL Command Output

  • The command below will result in an error because a group function is not allowed in the WHERE clause.

    SELECT department_id, AVG(salary)
    FROM employees
    WHERE AVG(salary) > 8000
    GROUP BY department_id;
    
  • If SELECT count(product_id) FROM order; gives 15 outputs, then SELECT count(DISTINCT product_id) FROM order; will give <= 15 outputs.

  • The SQL keyword used to combine the results of two or more SELECT statements is UNION.

  • LEFT JOIN returns all records from the left table and matching records from the right table.

  • The SQL keyword used to retrieve data from a database table is SELECT.

  • The SQL statement used to make permanent changes to the database is COMMIT.

  • In SQL, the operator used to check for matching values in a specified list is IN.

Select e.name, d.name 
FROM employees e 
JOIN departments d 
ON e.department_id = d.department_id;
  • The command above is equivalent to the command below
Select e.name, d.name 
FROM employees e 
NATURAL JOIN departments d;
  • Concat is a single-row function.
select substr('helloworld', -1, instr('helloworld', 'I', 3, 2)) from dual;
  • The output of the SQL function above is d.

  • SQL Aggregate functions always return single values.

  • In the CONCAT function, a literal can refer to a number, character, or date.

  • || is the symbol for the Concatenation Operator.

  • The query below finds the total number of employees working in 'HR' departments.

select count(*) 
from employees 
where department_id = (
  Select id 
  from departments
  where name = 'HR'
);
  • The query below finds the total rating of the sailors who have reserved boat 103
SELECT SUM(s.rating) 
FROM sailors s, reserves r 
WHERE s.sid = r.sid AND r.bid = 103;

Studying That Suits You

Use AI to generate personalized quizzes and flashcards to suit your learning preferences.

Quiz Team

Related Documents

SQL Practice Questions PDF

More Like This

SQL Database Concepts Quiz
5 questions

SQL Database Concepts Quiz

UndisputedChalcedony4145 avatar
UndisputedChalcedony4145
SQL Query Processing Example
22 questions
Tema 10: Introducción a SQL
64 questions
SQL Query Techniques
32 questions

SQL Query Techniques

WinningSanJose1484 avatar
WinningSanJose1484
Use Quizgecko on...
Browser
Browser