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

SQL Queries

The document provides SQL queries for selecting and deleting duplicate records in various tables, including Customer and Employee tables. It also outlines validation processes for mapping documents, database schema, and constraints, as well as examples of SQL operations such as finding the nth highest salary and listing employees with their supervisors. Additionally, it discusses types of constraints and includes examples of SQL joins and case statements.

Uploaded by

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

SQL Queries

The document provides SQL queries for selecting and deleting duplicate records in various tables, including Customer and Employee tables. It also outlines validation processes for mapping documents, database schema, and constraints, as well as examples of SQL operations such as finding the nth highest salary and listing employees with their supervisors. Additionally, it discusses types of constraints and includes examples of SQL joins and case statements.

Uploaded by

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

SELECT/ DELETE DUPLICATE RECORDS:-

============================================

Select CustomerID, count(*) as NumDuplicates


from Customer
group by CustomerID
having count(*) > 1

============================================

DELETE FROM EMP


WHERE
rowid NOT IN
(SELECT max(rowid) FROM EMP
GROUP BY emp_id);

============================================

SELECT *
FROM [SampleDB].[dbo].[Employee]
WHERE ID NOT IN
(
SELECT MAX(ID)
FROM [SampleDB].[dbo].[Employee]
GROUP BY [FirstName],
[LastName],
[Country] );

============================================

DELETE FROM [SampleDB].[dbo].[Employee]


WHERE ID NOT IN
(
SELECT MAX(ID) AS MaxRecordID
FROM [SampleDB].[dbo].[Employee]
GROUP BY [FirstName],
[LastName],
[Country]
);

=============================================
WITH CTE([FirstName],
[LastName],
[Country],
DuplicateCount)
AS (SELECT [FirstName],
[LastName],
[Country],
ROW_NUMBER() OVER(PARTITION BY [FirstName],
[LastName],
[Country]
ORDER BY ID) AS DuplicateCount
FROM [SampleDB].[dbo].[Employee])
DELETE FROM CTE
WHERE DuplicateCount > 1;
=============================================

DELETE E
FROM [SampleDB].[dbo].[Employee] E
INNER JOIN
(
SELECT *,
RANK() OVER(PARTITION BY firstname,
lastname,
country
ORDER BY id) rank
FROM [SampleDB].[dbo].[Employee]
) T ON E.ID = t.ID
WHERE rank > 1;

==============================================

Validation of Mapping Document


Validation of database Schema
Validation of constraints
Record count validation
Validation of correctness of data
Validation of transformation logic
Validation of data for duplication

==============================================
Types of Constraints:

NOT NULL
UNIQUE
DEFAULT
CHECK
Key Constraints – PRIMARY KEY, FOREIGN KEY
Domain constraints
Mapping constraints

===============================================
T1
1
1
2
3

T2
1
3
4
5
6
6
=====================================================

nth highest salary:


====================

select * from(
select e,pname, salary, dense_rank()
over(order by salary desc) Drank from Employee)
where Drank=n

======================================================
SELECT OrderID, ProductID, COUNT(*)
FROM OrderDetails
GROUP BY OrderID, ProductID
HAVING COUNT(*)>1

=======================================================
List employees along with the names of their
supervisors:

SELECT E.EmpCode, E.EmpName,


S.EmpCode, S.EmpName
FROM Emp E INNER JOIN Emp S
ON E.SupCode = S.EmpCode;

=======================================================

E1
3rows
1
2
0

Left Join
E2
6rows
1
1
2
2
2
5
=======================================================

select A.name,b.name,
case when c.marks>90 then expert
else intermediate as skill
from TableA A inner join TableC C on A.student_id= C.student_id
inner join TableB inner join TableC C on A.subject_id= C.subject_id

You might also like