0% found this document useful (0 votes)
7 views

SQL_Subqueries_Cpp_Presentation (1)

A subquery is a nested SQL query that returns data for the main query and can be used in various SQL commands like SELECT, INSERT, UPDATE, and DELETE. Examples include single-row, multiple-row, and correlated subqueries, which enhance SQL's expressiveness and power. Subqueries, when combined with C++, facilitate the development of dynamic, data-driven applications.

Uploaded by

alimuzammal529
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

SQL_Subqueries_Cpp_Presentation (1)

A subquery is a nested SQL query that returns data for the main query and can be used in various SQL commands like SELECT, INSERT, UPDATE, and DELETE. Examples include single-row, multiple-row, and correlated subqueries, which enhance SQL's expressiveness and power. Subqueries, when combined with C++, facilitate the development of dynamic, data-driven applications.

Uploaded by

alimuzammal529
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Slide 1: What is a Subquery?

- A subquery (inner query) is a query nested inside another SQL query.

- It returns data used by the main (outer) query.

Slide 2: Where Can You Use Subqueries?


Subqueries are commonly used in:

- SELECT

- INSERT

- UPDATE

- DELETE

Also used with: IN, =, ANY, ALL, EXISTS

Slide 3: SQL Subquery Example


SQL:

SELECT name FROM employees

WHERE salary > (SELECT AVG(salary) FROM employees);

Explanation:

Returns employees who earn more than the average salary.

Slide 4: C++ Code Example using SQLite


#include <iostream>

#include <sqlite3.h>

int main() {

sqlite3* DB;

sqlite3_open("company.db", &DB);
std::string sql = R"(

SELECT name FROM employees

WHERE salary > (SELECT AVG(salary) FROM employees);

)";

sqlite3_exec(DB, sql.c_str(), [](void*, int argc, char** argv, char**) -> int {

std::cout << "Employee: " << argv[0] << std::endl;

return 0;

}, nullptr, nullptr);

sqlite3_close(DB);

return 0;

Slide 5: Types of Subqueries


1. Single-row subquery - returns one value

2. Multiple-row subquery - returns a list

3. Correlated subquery - uses outer query values

Correlated Example:

SELECT name FROM employees e

WHERE salary > (

SELECT AVG(salary) FROM employees

WHERE department_id = e.department_id

);

Slide 6: Why Use Subqueries?


- Break down complex problems

- Reuse logic

- Write cleaner SQL

- Powerful filtering tool

Conclusion
Subqueries make SQL more expressive and powerful.

Combined with C++, they help create dynamic, data-driven applications.

You might also like