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

Warehouse and SQL QUESTIONS

Uploaded by

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

Warehouse and SQL QUESTIONS

Uploaded by

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

Warehouse and SQL QUESTIONS

Data warehouse concept:

1. Normalisation & Denormalization

2. Types of schemas(star and snowflake)

3. Measures and dimensions difference

4. Types of dimensions

5. Types of measures

6. Surrogate key

7. Constraints

8. Difference between OLTP and OLAP

9. SCD types

SQL:

1. Decode and case which is faster and its difference ?

2. Delete & truncate difference

3. View and materialized view difference

4. Delete, drop & truncate difference

5. inner join & intersect difference

6. Stored procedure and function difference

7. Types of indexes? & its types?

8. Types of Joins? & scenario based question

9. What is CTE table?

10. What is partition ? its types

11. What is analytical function & window function ?

12. B-tree and Bit map index difference


13. How to refresh materialized view?

14. INSTR(),SUBSTR() & Replace()?

15. Join and union difference

16. Union & Union all difference

17. NVL & NVL2 difference

18. Char,varchar & nvarchar difference

19. performance tuning technique

20. find nth salary

21. find duplicate salary

22. difference between rank() & Dense_rank()

23. Bulk insert or bulk bucket

24. DML,DDL,TCL

SQL

What is fact less fact table?


What is bridge dim/confirm dim table?

What is two type of schema in database?

What is surrogate key?

What is normalization?

What is foreign key?

How to delete dups? Delete from table


where rowid not in (sel max(rowid) from emp
group by id);

Q1> what are set operations

union, join, minus query


Q2> what is row number/id: a: sequence
number allocated to row

Q3> how to create same metadata/same


struture empty table

Q> command for see metadata


SELECT *
FROM T1
ORDER BY
CASE status
WHEN 'account' THEN 1
WHEN 'bad' THEN 2
[9:18 pm, 17/5/2024] Gauranga: I'd status WHEN 'nic' THEN 3
101 account WHEN 'iso' THEN 4
103 account ELSE 5
104 bad END;
102 nic [9:30 pm, 17/5/2024] Gauranga: SELECT
107 account id,
100 iso DECODE(status,
105 bad 'account', 1,
So we need status column like account 'bad', 2,
>bad>nic>iso in that order 'nic', 3,
'iso', 4
) AS status_order
FROM
your_table
ORDER BY
status_order;

SELECT distinct t.id,


CONCAT(COALESCE(t1.fname, ''),
COALESCE(t2.lname, '')) AS fullname
FROM em t
LEFT JOIN em t1 ON t.id = t1.id and t1.fname
id fname lname IS NOT NULL
1 sai null LEFT JOIN em t2 ON t.id = t2.id AND t2.lname
1 null ram IS NOT NULL

in above table they need output in below one OR by group by


line format using join
1 Sai ram
select id,CONCAT(max(fname), max(lname))
AS fullname
from em
group by id;
let's consider an example where we have a
table containing sales data for different
products in different regions. The table might
look something like this:

plaintext
Copy code SELECT
| Product | Region | Sales | Product,
|-----------|--------|-------| MAX(CASE WHEN Region = 'North' THEN
| Product A | North | 100 | Sales END) AS North,
| Product B | North | 150 | MAX(CASE WHEN Region = 'South' THEN
| Product A | South | 200 | Sales END) AS South,
| Product B | South | 250 | MAX(CASE WHEN Region = 'East' THEN
| Product A | East | 180 | Sales END) AS East
| Product B | East | 220 | FROM
Now, let's say we want to pivot this data so SalesData
that we have one row for each product, with GROUP BY
columns for sales in each region. The desired Product;
output would look like this: This query uses conditional aggregation to
pivot the data. It creates columns for each
plaintext region and fills in the sales amount for each
Copy code product in the corresponding column. Finally,
| Product | North | South | East | it groups the results by product.
|-----------|-------|-------|------|
| Product A | 100 | 200 | 180 |
| Product B | 150 | 250 | 220 |
Here's the SQL query to achieve this pivot:

SELECT *
FROM employees
You have a table with employee information
WHERE department_id IN (
and a separate table with department
SELECT department_id
information. You want to find all employees
FROM departments
who belong to a department with more than
GROUP BY department_id
a certain number of employees.
HAVING COUNT(*) > 10
);
You have a table with sales data and want to
calculate the cumulative sum of sales over
time.

SELECT date,
sales_amount,
SUM(sales_amount) OVER (ORDER BY
date) AS cumulative_sales
FROM your_table_name;

desc emp;

mysql> create table EMP_BKP2 as select *


from Employee where 1=0;

short in abinitio like order by in sql


rollup in abinitio like aggragate functions in
sql
second max sal --- sel max(sal) from emp
where sal > (sel max(sal) from emp)

Q4> count distinct records in a table.


select count(distint id) from emp

Q5> does unique constraint allow null valu?


yes

Q6>Does primary key constraint allow null


value?
Q> list of The employee hired 5 year back

Q7> what is foreign key constraint


The SQL FOREIGN KEY CONSTRAINT is used to
ensure the referential integrity of the data in
one table to match values in another table.

The FOREIGN KEY CONSTRAINT is a column or


list of columns which points to the PRIMARY
KEY of another table.

The main purpose of FOREIGN KEY is, only


those values will appear which are present in
the primary key table.
For each row in the referencing table( the
table contains the FOREIGN KEY), the foreign
key must match an existing primary key in
the referenced table(the table contains the
PRIMARY KEY). This enforcement of FOREIGN
KEY called the Referential Integrity.

The structure and data type of PRIMARY KEY


and FOREIGN KEY must be same.

The values of the FOREIGN KEY columns in


each row of the referencing table have to
match with the values of the corresponding
primary key columns of a row in the
referenced table.
i/p select dname,(select count(*) from dept1 d
where d.gender='M' and d.dname=e.dname
depid gender group by d.dname ) as Male_count,
IT M (select count(*) from dept1 d where d.gender='F'
IT F and d.dname=e.dname group by d.dname ) as
IT F Female_count
CS M from dept1 e group by e.dname;
CS F
select dname, count(case when gender='M' then
1 end )as male, count(case when gender='F'then
o/p 1 end )as female
deptid male_count female_count
IT 1 2
CS 1 1 from dept1 e group by e.dname;

A
B
C
D

OP SELECT
AB l1.letter, l2.letter
AC FROM
AD (VALUES ('A'), ('B'), ('C'), ('D')) l1 (letter)
BC JOIN
BD (VALUES ('A'), ('B'), ('C'), ('D')) l2 (letter) ON
l1.letter < l2.letter
Suppose you have a database with two tables:
employees and performance_reviews. The
employees table contains information about
employees, and the performance_reviews table
contains performance review scores for each
employee. Your task is to retrieve the names of
employees who have a higher average
performance score than the overall average
score for all employees.

employees table:
+------+-----------+
| emp_id | emp_name |
+------+-----------+
| 1 | Alice |
| 2 | Bob | Answer:
| 3 | Charlie |
| 4 | David | sql
+------+-----------+ Copy code
performance_reviews table: SELECT e.emp_name
FROM employees e
diff JOIN (
Copy code SELECT emp_id, AVG(score) AS avg_score
+------+-----------+ FROM performance_reviews
| emp_id | score | GROUP BY emp_id
+------+-----------+ ) pr ON e.emp_id = pr.emp_id
| 1 | 85 | WHERE pr.avg_score > (SELECT AVG(score) FROM
| 2 | 92 | performance_reviews);
| 3 | 78 |
| 4 | 90 |
| 1 | 88 |
| 2 | 95 |
| 3 | 80 |
| 4 | 92 |
+------+-----------+
Question:
Write an SQL query to retrieve the names of
employees who have an average performance
score higher than the overall average score for all
employees.

dept deptid dept name


emp empid empname empsal deptid managid
1,a,100,10,4
2,b,100,10,4 SELECT e.empid, e.empname, e.managid,
3,c,100,5,2 m.empname AS managname
FROM emp e
empid empname managid managname LEFT JOIN emp m ON e.managid = m.empid;
max-core For Abinitio Components
max-core -
The max-core parameter is found in the SORT,
JOIN, and ROLLUP components, among others.

max-core Value -
There is no single optimal value for the max-core
parameter. A “good” value depends on your
particular graph and the environment in which it
runs, as well as on the data.

Details -
A component’s max-core parameter determines
the maximum amount of memory the component
will consume per partition before it spills to disk.
When the value of max-core is exceeded, all input
(in the case of SORT) or the excess input (in the
cases of the other components) is dropped to
disk in the form of temporary files. This can have
a dramatic impact on performance, but it does
not mean that it is always better to increase the
value of max-core in these situations.

The higher you set the value of max-core, the


more memory the component can use. Using
more memory generally improves performance
How to calc max_core value.? What will happen if — up to a point.
max_core value is less or more then req. Beyond this point, performance will not improve
and may even worsen.
If the value of max-core is set too high, operating
system swapping can occur; if virtual memory on
the machine is exhausted, the graph can fail.

When setting the value for max-core, we can use


the suffixes k, m, and g (uppercase is also
supported) to indicate powers of 1024.
For max-core, the suffix k (kilobytes) means
precisely 1024 bytes. Similarly, the suffix m
(megabytes) means precisely 1048576 (1024
power 2),
and g (gigabytes) means precisely 1024 power 3.

In general, using additional memory can improve


the performance of in-memory ROLLUP or JOIN,
but not of SORT.

When spillage occurs, consider setting the


configuration variable
AB_SPILL_FILE_COMPRESSION_LEVEL.
This variable compresses the temporary files
spilled to the disk. It is most helpful when you
have a fast CPU but a slow disk (which is
common).

SORT component -
For the SORT component, 96 MB (100663296
bytes) is the default value for max-core.

employees table:
employee_id (int, primary key)
employee_name (varchar)
department (varchar) SELECT
salaries table: e.department,
employee_id (int, foreign key referencing AVG(s.salary_amount) AS average_salary
employees.employee_id) FROM
salary_amount (decimal) employees e
Write an SQL query to find the average salary for JOIN salaries s ON e.employee_id =
each department. Utilize an aggregate subquery s.employee_id
to calculate the average salary for each GROUP BY
department. e.department;

products table: SELECT


product_id (int, primary key) p.category,
product_name (varchar) SUM(s.sale_amount) AS total_revenue,
category (varchar) CASE
sales table:
product_id (int, foreign key referencing
products.product_id) WHEN SUM(s.sale_amount) > 10000 THEN
quantity_sold (int) 'High Revenue'
sale_amount (decimal) ELSE 'Low Revenue'
Write an SQL query to calculate the total revenue END AS revenue_label
for each category, but with a condition: if the FROM
total revenue is greater than a certain threshold products p
(let's say $10,000), label it as "High Revenue," JOIN sales s ON p.product_id = s.product_id
otherwise label it as "Low Revenue." GROUP BY
p.category;

SELECT
department,
AVG(performance_rating) AS avg_rating,
employee_ratings table: CASE
employee_id (int, primary key) WHEN AVG(performance_rating) >= 4 THEN
performance_rating (int) 'High Performer'
department (varchar) WHEN AVG(performance_rating) >= 3 AND
Write an SQL query to categorize employees AVG(performance_rating) < 4 THEN 'Medium
based on their average performance rating within Performer'
their department. Assign labels such as "High ELSE 'Low Performer'
Performer" if the average rating is greater than or END AS performance_category
equal to 4, "Medium Performer" if the average FROM
rating is between 3 and 4 (inclusive), and "Low employee_ratings
Performer" if the average rating is less than 3. GROUP BY
department;

CREATE TABLE products (


product_id INT PRIMARY KEY,
product_name VARCHAR(50),
category_id INT,
price DECIMAL(10, 2) SELECT
); p.product_id,
p.product_name,
CREATE TABLE categories ( p.price,
category_id INT PRIMARY KEY, p.category_id,
category_name VARCHAR(50) (SELECT AVG(price) FROM products WHERE
); category_id = p.category_id) AS
Write a SQL query to retrieve the product details average_category_price
along with the average price of all products in the FROM
same category. products p;

SELECT
Scenario 1: Counting Male and Female Customers COUNT(CASE WHEN gender = 'M' THEN 1 END)
AS male_count,
customer_id, customer_name, gender COUNT(CASE WHEN gender = 'F' THEN 1 END)
(1, 'John Doe', 'M'),
(2, 'Jane Smith', 'F'),
(3, 'Bob Johnson', 'M'),
(4, 'Alice Brown', 'F'),
(5, 'Charlie Davis', 'M'),
(6, 'Eva White', 'F');

O/p
AS female_count
| male_count | female_count | FROM
|------------|--------------| customers;
|3 |3 | Expected Result:

I/P
Scenario 2: Counting Orders with Different Status
order_id, customer_id, order_date, order_status)
VALUES
(101, 1, '2024-02-01', 'Pending'),
(102, 2, '2024-02-02', 'Shipped'),
(103, 3, '2024-02-03', 'Delivered'), SELECT
(104, 4, '2024-02-04', 'Shipped'), COUNT(CASE WHEN order_status = 'Pending'
(105, 5, '2024-02-05', 'Pending'), THEN 1 END) AS pending_count,
(106, 6, '2024-02-06', 'Delivered'); COUNT(CASE WHEN order_status = 'Shipped'
THEN 1 END) AS shipped_count,
O/P COUNT(CASE WHEN order_status = 'Delivered'
| pending_count | shipped_count | THEN 1 END) AS delivered_count
delivered_count | FROM
|---------------|---------------|------------------| orders;
|2 |2 |2 | Expected Result:

Jio
jioTV
JioCloud
AJio
MyJio
JioMart
Write sQL query for fetch all Jio related apps…
and write unix cmd as well for the same

You might also like