SQL Flashcards

1
Q

What is the difference between DELETE and TRUNCATE?

A

DELETE is a DML command and TRUNCATE is a DDL command

The best image of the difference is that:

if you do DELETE TABLE, this would remove all the rows of data from a table while keeping the tables structure.

TRUNCATE TABLE would completely get rid of the table.

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

Difference between DML and DDL?

A

DML stands for data manipulation language. It is used to manipulate the data itself.

Insert, Update, Select, and Delete statements are all a part of DML.

DDL stands for data definition language which is used to define data structures.

create table, alter table, drop, rename and alter are all DDL statements

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

Difference between where and having?

A

both commands filter the table to meet a certain condition.

Having is used with the Group By statement to filter rows after collecting Whereas where is used without the Group By.

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

What is an index in SQL? When would you use an index?

A

indexes are lookup tables used by the database to perform data retrieval more efficiently.

This will speed up SELECT and WHERE clauses but slow down UPDATE and INSERT.

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

What are aggregate functions in SQL?

A

An aggregate function performs a calculation on a set of values and returns a single value.

Three common ones are COUNT, SUM and AVG.

AVG ignores Null values

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

What SQL commands are utilized in ETL?

A

SELECT, JOIN, WHERE, ORDER BY, GROUP BY

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

Does JOIN order affect SQL query performance?

A

Yes, if you have 3 tables. two large and one small. You join the two large together and then the small. The query is going over more rows of data so it is using more processing.

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

How do you change a column name by writing a query in SQL?

A

ALTER TABLE TableName
RENAME COLUMN OldColumnName TO NewColumnName;

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

How do you handle duplicate data in SQL?

A

Okay so to clarify for this question:

what kind of data is being processed?
what type of values can users duplicate?

Distinct key on primary_key of the table which is usually an id of some sort.

You can also use the Group By function on two values

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

Most complicated SQL query you wrote recently

A

Sure just before this call I wrote one that was pretty complicated.

I had to find the top 3 customers we had by revenue for each year beggening in 2019 for a report.

The reason this was

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

How to add Partitions?

A

ALTER TABLE sales
ADD PARTITION jan99 VALUES LESS THAN ( ‘01-FEB-1999’ )
TABLESPACE tsx;

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

How to select a partition?

A

mysql> SELECT * FROM employees PARTITION (p1);
+—-+——-+——–+———-+—————+
+—-+——-+——–+———-+—————+
| 5 | Mary | Jones | 1 | 1 |
| 6 | Linda | Black | 2 | 3 |
| 7 | Ed | Jones | 2 | 1 |
| 8 | June | Wilson | 3 | 1 |
| 9 | Andy | Smith | 1 | 3 |
+—-+——-+——–+———-+—————+
5 rows in set (0.00 sec)

id | fname | lname | store_id | department_id |

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

How to update a row in SQL?

A

UPDATE Customers
SET ContactName = ‘Alfred Schmidt’, City= ‘Frankfurt’
WHERE CustomerID = 1;

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

How to insert values into SQL?

A

INSERT INTO Customers (CustomerName, ContactName, Address, City, PostalCode, Country)
VALUES (‘Cardinal’, ‘Tom B. Erichsen’, ‘Skagen 21’, ‘Stavanger’, ‘4006’, ‘Norway’);

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

how to insert a new column conditionally in SQL?

A

ALTER TABLE person
ADD salary int(20);
UPDATE persons SET salary = ‘145000’ where Emp_Id=12;

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

How to update a table conditionally in SQL??

A

UPDATE EMPLOYEE
SET age =
CASE WHEN AGE < 20 THEN 15
ELSE 20 END

17
Q

What is the difference between an Index and a partition?

A

index’s speed the search of data within tables. parttitions actually subdivide the table into subdirectorys.

In geenral index’s are better for small amounts of data and partitions are better for large amounts of data