From GPT Flashcards

1
Q

What is the purpose of a SELECT statement in SQL?

A

The purpose of a SELECT statement is to retrieve specific columns or all columns from one or more tables.

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

Write the basic syntax for a SELECT statement.

A

SELECT column1, column2 FROM table_name;

SELECT name, position FROM employees;

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

How do you filter records in SQL to include only those that meet specific conditions?

A

By using the WHERE clause.
Syntax: SELECT column1, column2 FROM table_name WHERE condition;

SELECT name, age FROM employees WHERE department = 'HR';

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

What is an INNER JOIN used for?

A

An INNER JOIN is used to return rows when there is at least one match in both tables.

SELECT e.name, d.department_name FROM employees e INNER JOIN departments d ON e.department_id = d.id;

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

Provide an example of an INNER JOIN statement.

A

SELECT e.name, d.department_name FROM employees e INNER JOIN departments d ON e.department_id = d.id;

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

What is the purpose of a LEFT JOIN?

A

A LEFT JOIN returns all rows from the left table, and the matched rows from the right table.

SELECT e.name, d.department_name FROM employees e LEFT JOIN departments d ON e.department_id = d.id;

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

How can you group rows that have the same values in specified columns?

A

By using the GROUP BY clause.
Syntax: SELECT column, COUNT(*) AS alias FROM table_name GROUP BY column;

SELECT department, COUNT(*) AS num_employees FROM employees GROUP BY department;

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

What is the purpose of the HAVING clause?

A

The HAVING clause filters groups based on aggregated results.

SELECT department, COUNT(*) AS num_employees FROM employees GROUP BY department HAVING num_employees > 3;

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

How do you sort the result set of a query by one or more columns?

A

By using the ORDER BY clause.
Syntax: SELECT column1, column2 FROM table_name ORDER BY column1 DESC;

SELECT name, salary FROM employees ORDER BY salary DESC;

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

Write an example of an INSERT statement.

A

INSERT INTO employees (name, age, department) VALUES ('Alice Green', 28, 'Marketing');

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

How can you modify existing records in a table?

A

By using the UPDATE statement.
Syntax: UPDATE table_name SET column1 = value1 WHERE condition;

UPDATE employees SET age = 29 WHERE name = 'Alice Green';

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

What is the purpose of the DELETE statement in SQL?

A

The DELETE statement is used to delete existing records from a table.

DELETE FROM employees WHERE name = 'Alice Green';

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

Provide an example of creating a table with a primary key.

A

CREATE TABLE departments ( id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(100) );

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

Name 3 types of functions that can be used in SQL queries.

A
  1. String functions
  2. Date functions
  3. Numerical functions

SELECT name, ROUND(salary, 0) AS rounded_salary FROM employees;

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

Write an example of using a subquery in a SQL statement.

A

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

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

How do you create a temporary table in SQL?

A

CREATE TEMPORARY TABLE high_earners AS SELECT name, salary FROM employees WHERE salary > 80000; SELECT * FROM high_earners;

17
Q

What is the purpose of creating indices in SQL?

A

Indices are created to improve the performance of query operations.

CREATE INDEX idx_salary ON employees(salary);

18
Q

Which tools can be used for basic reporting and data visualization with SQL databases?

A
  • Tableau: Connect to MySQL, extract employee data, and create interactive dashboards.
  • Power BI: Import MySQL data, model it, and share insights through rich visualizations.
  • Excel: Use ODBC to pull data into spreadsheets for further analysis and charting.
19
Q

How do you manage user access and security in SQL?

A

By using the GRANT statement.
Syntax: GRANT SELECT, INSERT ON database_name.table_name TO 'username'@'host';

GRANT SELECT, INSERT ON company_db.employees TO 'jdoe'@'localhost';