What are the TRUNCATE, DELETE and DROP statements?
DELETE statement is used to delete rows from a table.
DELETE FROM Candidates
WHERE CandidateId > 1000;
TRUNCATE command is used to delete all the rows from the table and free the space containing the
table.
TRUNCATE TABLE Candidates;
DROP command is used to remove an object from the database. If you drop a table, all the rows in
the table are deleted and the table structure is removed from the database.
DROP TABLE Candidates;
Write a SQL statement to wipe a table 'Temporary' from memory.
Write a SQL query to remove first 1000 records from table 'Temporary' based on 'id'.
Write a SQL statement to delete the table 'Temporary' while keeping its relations intact.
39. What is the difference between DROP and TRUNCATE statements?
If a table is dropped, all things associated with the tables are dropped as well. This includes - the
relationships defined on the table with other tables, the integrity checks and constraints, access
privileges and other grants that the table has. To create and use the table again in its original form,
all these relations, checks, constraints, privileges and relationships need to be redefined. However, if
a table is truncated, none of the above problems exist and the table retains its original structure.
40. What is the difference between DELETE and TRUNCATE statements?
The TRUNCATE command is used to delete all the rows from the table and free the space containing
the table.
The DELETE command deletes only the rows from the table based on the condition given in the
where clause or deletes all the rows from the table if no condition is specified. But it does not free
the space containing the table.
What are Aggregate and Scalar functions?
An aggregate function performs operations on a collection of values to return a single scalar value.
Aggregate functions are often used with the GROUP BY and HAVING clauses of the SELECT statement.
Following are the widely used SQL aggregate functions:
AVG() - Calculates the mean of a collection of values.
COUNT() - Counts the total number of records in a specific table or view.
MIN() - Calculates the minimum of a collection of values.
MAX() - Calculates the maximum of a collection of values.
SUM() - Calculates the sum of a collection of values.
FIRST() - Fetches the first element in a collection of values.
LAST() - Fetches the last element in a collection of values.
Note: All aggregate functions described above ignore NULL values except for the COUNT function.
A scalar function returns a single value based on the input value. Following are the widely used SQL
scalar functions:
LEN() - Calculates the total length of the given field (column).
UCASE() - Converts a collection of string values to uppercase characters.
LCASE() - Converts a collection of string values to lowercase characters.
MID() - Extracts substrings from a collection of string values in a table.
CONCAT() - Concatenates two or more strings.
RAND() - Generates a random collection of numbers of a given length.
ROUND() - Calculates the round-off integer value for a numeric field (or decimal point
values).
NOW() - Returns the current date & time.
FORMAT() - Sets the format to display a collection of values.
------------------------------------------------------------------------------------------------------------------------------------
WHERE: Filters rows before any grouping takes place.
HAVING: Filters grouped data after the GROUP BY clause has been applied.
In short, WHERE applies to individual rows, while HAVING applies to groups.
How to find Nth Highest Salary
SELECT DISTINCT Salary FROM Employee ORDER BY Salary DESC LIMIT 4, 1
);
Find the Second Highest Salary in SQL
SELECT DISTINCT salary
FROM employees
ORDER BY salary DESC
LIMIT 1;
Method 2: Using Subquery
sql
SELECT MAX(salary) AS SecondHighestSalary
FROM employees
WHERE salary < (SELECT MAX(salary) FROM employees);
DENSE RANK
SELECT salary
FROM (
SELECT salary, DENSE_RANK() OVER (ORDER BY salary DESC) AS rank
FROM employees
) temp
WHERE rank = 2;
Identify Duplicate Records
SELECT GeekRank, COUNT(GeekID) AS DuplicateRanks
FROM Geeks
GROUP BY GeekRank
HAVING COUNT(GeekRank)>1;
Identifying Duplicate Rows B
based on a Single Column
SELECT email, COUNT(*) AS duplicate_count
FROM customers
GROUP BY email
HAVING COUNT(*) > 1;
Finding Duplicates Based on Multiple Columns
SELECT first_name, last_name, COUNT(*) AS duplicate_count
FROM contacts
GROUP BY first_name, last_name
HAVING COUNT(*) > 1;
SELECT DISTINCT column1, column2
FROM table_name;
https://www.baeldung.com/sql/identify-duplicate-values
EXCEL
you customize a message with the XLOOKUP function if the lookup value is not found,
Extract Data Based on the Last Occurrence with XLOOKUP Only
VLOOKUP would give a wrong result if you add/delete a new column in your data
If speed is what you are looking for, INDEX/MATCH combo is the way to go.
If backward compatibility is required, INDEX + MATCH is the most flexible and powerful lookup
option available. XLOOK UP INTODUCED IN 2021
Reverse search. While INDEX MATCH can only look up from the beginning to the end, both XLOOKUP
and INDEX + XMATCH can search in both directions: from first to last and from last to first.
Handling errors. The INDEX MATCH combo does not have a built-in feature to handle errors. If the
formula cannot find a lookup value, it will show #N/A. On the other hand, XLOOKUP has an extra
argument (if_not_found) that lets you customize the response when there is no match. You can
provide a certain value, display a friendly message, or even execute another formula.
The key benefits of XLOOKUP are:
It is simpler and shorter to write than INDEX MATCH.
It can perform lookups in any direction: top-to-bottom, bottom-to-top, left-to-right as well as
right-to-left.
It can do both exact and approximate matches in any dataset, whereas INDEX MATCH is
limited to approximate matches in sorted data.
It can handle dynamic arrays and spill results to multiple cells.
It can natively handle errors caused by missing values.
Some of the drawbacks of XLOOKUP are:
It is not compatible with older versions of Excel 2019 and lower.
It may not be as flexible as INDEX MATCH for some scenarios, such as returning values from
non-contiguous columns.
It may not work well with large data sets or volatile formulas.
How to handle null values