Accenture SQL Interview Question | Database Case Sensitivity vs Insensitivity
Вставка
- Опубліковано 2 жов 2024
- In this video we will discuss a Accenture SQL interview question and solve it when the data is case sensitive and when it is insensitivity .
Here is the script:
CREATE TABLE employees (employee_id int,employee_name varchar(15), email_id varchar(15) );
delete from employees;
INSERT INTO employees (employee_id,employee_name, email_id) VALUES ('101','Liam Alton', 'li.al@abc.com');
INSERT INTO employees (employee_id,employee_name, email_id) VALUES ('102','Josh Day', 'jo.da@abc.com');
INSERT INTO employees (employee_id,employee_name, email_id) VALUES ('103','Sean Mann', 'se.ma@abc.com');
INSERT INTO employees (employee_id,employee_name, email_id) VALUES ('104','Evan Blake', 'ev.bl@abc.com');
INSERT INTO employees (employee_id,employee_name, email_id) VALUES ('105','Toby Scott', 'jo.da@abc.com');
INSERT INTO employees (employee_id,employee_name, email_id) VALUES ('106','Anjali Chouhan', 'JO.DA@ABC.COM');
INSERT INTO employees (employee_id,employee_name, email_id) VALUES ('107','Ankit Bansal', 'AN.BA@ABC.COM');
ALTER TABLE employees
ALTER COLUMN email_id VARCHAR(15) COLLATE SQL_Latin1_General_CP1_CS_AS;
Zero to hero(Advance) SQL Aggregation:
• All About SQL Aggregat...
Most Asked Join Based Interview Question:
• Most Asked SQL JOIN ba...
Solving 4 Trick SQL problems:
• Solving 4 Tricky SQL P...
Data Analyst Spotify Case Study:
• Data Analyst Spotify C...
Top 10 SQL interview Questions:
• Top 10 SQL interview Q...
Interview Question based on FULL OUTER JOIN:
• SQL Interview Question...
Playlist to master SQL :
• Complex SQL Questions ...
Rank, Dense_Rank and Row_Number:
• RANK, DENSE_RANK, ROW_...
#sql #dataanalytics
Please like the video for more interview questions.
with cte as (select *, count(*) over(partition by lower(email_id)) as cnt
from employees)
select * from cte
where cnt = 1 or lower(email_id) = email_id
Hello Ankit,
This is my solution.
WITH CTE AS(
SELECT *,
COUNT(*) OVER(PARTITION BY LOWER(email_id)) AS distincts,
CASE WHEN email_id = LOWER(email_id) THEN 1 ELSE 0 END AS FLAG
FROM employees)
SELECT employee_id, employee_name, email_id FROM CTE WHERE
(distincts = 1 AND flag = 0) OR flag = 1
will work only in case of case sensitive
What is the Power Of CTE, I got to know after watching your video, Lovely explaination, BHaiya aapke CTE ke video dekhne ke baad my confidence in CTE is on the top of the floor.
It's my pleasure🙏
This will work ?
not sure ? any one confirm on this
delete from emploee
where id not in (
select id from emp E join F
on E. email_id = F. email_id
where E.ID > F.ID
and E.email_id not in (select upper(email_id) from emp )
)
parition by lower/upper(email_id) will work for both case sensitive and insensitive email_ids. Correct me If I'm wrong.
Thank you.Nice Explanation.
Please let me know your opinion on my following answer, taking advantage of the rank() function:
WITH cte
(SELECT *, ROW_NUMBER() OVER (partition by email_id) AS rnu, RANK() OVER (partition by email_id) AS rnk,
FROM employees)
SELECT employee_id, employee_name, email_id FROM cte
WHERE rnu = rnk
ORDER BY email_id;
@AnkitBansal , waiting for the Redshift course ❤
with temp as (
Select lower(email_id) as l_email_id, count(1)
from employees
group by lower(email_id)
having count(1) >= 2
)
select * from employees where email_id not in (select UPPER(l_email_id) from temp)
Thanks for explaining step by step real-time interview question, Request you please bring more real-time interview question
delete from employees
where lower(email_id) IN(
select lower(email_id) from employees
group by lower(email_id)
having count(lower(email_id)) > 1)
with cte1 as (select
emp_id,
email_id,
count(email_id) over (partition by email_id)as cnt
from eid),
cte2 as (select emp_id,email_id from cte1 where cnt>1)
SELECT * FROM cte2
WHERE UPPER(email_id) COLLATE Latin1_General_CS_AS email_id;
SELECT temp.employee_id, temp.employee_name, temp.email_id FROM employees temp
MINUS
SELECT temp1.employee_id, temp1.employee_name, temp1.email_id FROM (SELECT DISTINCT employee_id, employee_name, email_id ,dense_rank() over(partition by lower(email_id) order by employee_id) as r from employees) TEMP1 WHERE
TEMP1.R>1 AND TEMP1.EMAIL_ID=UPPER(TEMP1.EMAIL_ID);
with cte as (select*, ascii(email_id)as ascaii,
rank() over(partition by email_id order by ascii(email_id) desc) as rn
from employees)
select employee_id, employee_name, email_id from cte where rn=1
WITH CTE AS
(
SELECT *,
RANK( )OVER(PARTITION BY EMP_MAIL ORDER BY EMP_NAME DESC) AS EMP_RANK
FROM DUPLICATE_VALUES
),
FINAL AS
( SELECT *,
RANK()OVER (PARTITION BY EMP_RANK ORDER BY EMP_NAME DESC ) AS FINAL_EMP_RANK
FROM CTE
)
SELECT TOP 2 EMP_ID,EMP_NAME,EMP_MAIL
FROM FINAL
WHERE FINAL_EMP_RANK=1
with cte as(
select *,count(*) over(partition by
email_id) cnt
from employees
)
select employee_id,employee_name, email_id from cte
where cnt=1
or(cnt>1 and lower(email_id)
COLLATE Latin1_General_CS_AS=email_id
)
order by employee_id;
Awesome, By the way I made this query:
select * from employees
where ASCII(email_id) ASCII(upper(email_id))
union
select * from employees where email_id in (
select email_id from employees
group by email_id
having count(email_id) = 1
)
select * from
(
select *,lower(email_id) as l_email ,ASCII(email_id) as ASCI,
row_number() over(partition by lower(email_id) order by ASCII(email_id) desc) as rw
from employees
)A
where ASCI>97 or rw=1
My Sql Solution: with cte as (
select employee_id, employee_name, email_id,count(*) over (partition by email_id) as cnt from employees
)
, cte2 as (
select employee_id, employee_name, lower(email_id) as lw, email_id from cte
where cnt > 1
)
select * from cte2
where ascii(lw) = ascii(email_id)
Thanks Ankit, here is my solution:
with cte as (
select *,
case when regexp_like(email_id, '.*[A-Z]+.*') then 1 else 0 end as cap_ind,
count(lower(email_id)) over(partition by lower(email_id)) as cnt_email
from employees
)
select *
from cte
where not (cap_ind = 1 and cnt_email > 1);
A humble request to you plj use some hindi some eng by which I can understand easily
WITH cte AS(
SELECT DISTINCT EMAIL_ID FROM (
SELECT
EMPLOYEE_ID
,EMPLOYEE_NAME
,UPPER(EMAIL_ID) AS EMAIL_ID
,ROW_NUMBER() OVER(PARTITION BY UPPER(EMAIL_ID) ORDER BY UPPER(EMAIL_ID)) AS rn
FROM employees
)
WHERE RN >1
),cte2 AS(
SELECT
x.EMPLOYEE_ID
,x.EMPLOYEE_NAME
,x.EMAIL_ID
--,y.email_id
FROM employees x
INNER JOIN cte y
ON x.EMAIL_ID = y.EMAIL_ID
)
SELECT * FROM employees WHERE EMPLOYEE_ID NOT IN (SELECT EMPLOYEE_ID FROM cte2);
this question asked for freshers or experienced?? ankit bhaiya
It probably asks for an experience candidate.
what if duplicate upper case emaild is getting rank as 1?
Great!!!
It was a wonderful explanation .
If possible can you make a performance tuning videos
will it be correct to use dense_rank() over here instead of rank()?
I found gold!
Awesome..as usually..
Thank you! Cheers!
with cte as(
select *,case when lower(email_id)!=email_id then 'uppercase' else 'lowercase' end as caser,row_number() over(partition by lower(email_id) order by employee_id) as rn from employees
)
select * from cte where rn
this is wrong since it already treating it as same
@@ishika7585 Not sure what you are expecting but i am getting the right results. There are 2 filters one for case validation and second for rank. Please let me know if there is a gap in my answer.
hi ankit how about this query ? select distinct (ascii(email_id)), email_id from employees
We need all the columns. Emp I'd name etc
THE SQL ARTIST ANKIT BANSAL❤❤❤
hello Ankit,can you please help me out i am also giving data egineer interview and failing in sql part..please give me suggestion how to make it strong in sql. please.
Just complete this course :
www.namastesql.com/
@@ankitbansal6 is it paid?
Best solution
Splendid 🎉
select * from employees
where employee_id not in (
select employee_id from (
select employee_id, employee_name, email_id, upper(email_id) as U_email_id , count(1) over(partition by upper(email_id) ) as cont
from employees) a
where email_id=upper(email_id)
and cont>1)
order by employee_id;
Hi Ankit,
Very well explained! Can we use dense_rank here instead of rank()?
My Query: using CTE and Window function.
WITH CTE AS(
SELECT
*,
CASE
WHEN COUNT(*) OVER (PARTITION BY email_id) > 1 THEN 0
ELSE 1
END AS IsDuplicate
FROM
Employees
)
SELECT employee_id,employee_name, email_id FROM CTE
where isDuplicate = 0
ORDER BY employee_id
After trying myself I have checked your video. I can see there are some edge case also you have given by adding your name. Thanks today have learned new about case sensitivity.
I have a burning question that need to be answered by Industry expert.As we all know AI is becoming super advance so my question is why we put our effort to learn these sql while ai can do it faster and better way...please answer this anyone who has knowledge on this
-- is this correct?
SELECT e1.employee_id FROM employees e1 INNER JOIN
(SELECT LOWER(email_id) AS email_id,count(1) AS count FROM employees GROUP BY lower(email_id) HAVING count(1) > 1) e2 ON e1.email_id = e2.email_id
ORDER BY e1.employee_id