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

Word SQL

Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

Word SQL

Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

1. How to find no of employees in each department…?

Select department, count(empname) as Employeecount from Emp_tb group by(department)

2. Write a query to find odd number of records…?


select * from (select *, row number() over(order by empid) as rowno from Employeeinfo) as
oddquery where rowno%2 !=0

3. NTH HIGHEST SALARY

SELECT EMPNO, ENAME, SAL DENSE_RANK() OVER (ORDER BY SAL DESC) AS RN FROM Emp.

4. 2ND HIGHEST SALARY


SELECT A.EMPNO, A.ENAME, A.SAL FROM (

SELECT EMPNO, ENAME, SAL DENSE_RANK() OVER (ORDER BY SAL DESC) AS RN

FROM Emp) AS A WHERE RN=2

5. VIEWS
IT IS A LOGICAL REPRESENTATION OF ONE OR MORE TABLES. A view can also be considered as a virtual
table. View cant store the data.

CREATE VIEW NAME AS SELECT COLUMN1, FROM TABLENAME WHERE CONDITION

6. DELETE

REMOVE SOME OR ALL ROWS [DML] DELETE * FROM EMP

7. TRUNCATE

REMOVES ALL THE ROWS FROM THE TABLE [DDL]. YOU CAN’T SPECIFY WHERE CLAUSE.

TRUNCATE TABLE EMP

8. DROP

REMOVES TABLE FROM THE DATABASE

ONCE YOU DROP THE TABLE YOU CAN'T REVERSE BACK

DROP TABLE EMP

9. SQL QUERY TO FIND ALL DUPLICATE EMAILS IN A TABLE.


SELECT EMAIL [COLUMN NAME]

FROM PERSON [TABLE NAME]

GROUP BY EMAIL [COLUMN NAME]

HAVING COUNT(EMAIL)>1
10. Normalization in SQL?

Normalization divides the larger table into smaller tables and links them using relationship.

11. SELF JOIN:-

SELECT A.ID, A.NAME AS EMP_NAME, B.NAME AS MANAGER_NAME FROM EMP_TB A LEFT JOIN
EMP_TB B ON A.M_ID=B.ID;

SELECT A.ID,A.F_NAME AS EMP_NAME , B.F_NAME AS MANAGER_NAME FROM EMP_TB A,EMP_TB B


where A.M_ID=B.ID order by ID;

12. PARTION BY:-

SELECT STUDENT_ID,NAME,CLASS FROM(

SELECT STUDENT_ID, NAME, CLASS, MARKS, DENSERANK() OVER(PARTITION BY CLASS ORDER BY


MARKS DESC) AS RNK FROM STUDENT) T WHERE RNK=1 (FOR ONLY FIRST RANK OF STUDENT)

SELECT *,RANK() OVER(PARTITION BY CLASS ORDER BY MARKS DESC) AS RNK FROM STUDENT;

13.I want sum of salary and name and name start with like H and salary<5000…?

select sum(salary),name from employee where name like '%h' groupby name having sum(salary)<5000;

14. I want count of state from country…?

select state,count(city) as city count from sales group by state;

STRUCTURE OF SQL
1. CREATE , SELECT, FROM , WHERE , GROUP BY , 1.HAVING , ORDER BY.
Order of execution of Sql query…?

FROM, WHERE, GROUP BY, HAVING, SELECT, ORDER BY, Limit.

15.CTE :- WITH CTE AS(

SELECT *, RANK() OVER(ORDER BY MARKS DESC) RNK FROM STUDENT_MARKS)

SELECT * FROM CTE WHERE RNK=3;

16. I WANT 1ST HEIGHEST MARKS IN STUDENT NAME

SELECT ID,NAME,MAX(MARKS) FROM STUDENT_MARKS;

17. DIFF B/W PROCEDURES AND FUNCTIONS:-

PROCEDURE:- FUNCTIONS:-

1. FOR REPETATIVE TASK 1.BUSINESS LOGICS

2. CAN OR CANNOT HAVE INPUT PARAMETERS (IN) 2. INPUT IS MANDATORY (IN)

3. NEED NOT RETURN OUTPUT. 3. OUTPUT IS MANDATORY (RETURN/RETURNS)


4. CAN BE CALLED FROM CALL. 4.CAN BE CALLED FROM SELECT.

* Longest city and short city in sql.[Lenth]


Aggregation speed should be faster in sub-query rather than group by.
18.DIFFERENCE BETWEEN DBMS AND RDBMS:-
1.IN DBMS DATA STORE AS A FILE 1.IN RDBMS DATA STORE IN A TABULAR FORM

2.IT DOES NOT SUPPORT CLIENT 2.IT SUPPORT CLIENT SERVER ARCHITECTURE.
SERVER ARCHITECTURE.
3.NORMALIZATION IS NOT 3.NORMALIZATION IS AVAILABLE IN RDBMS.

AVAILABLE IN DBMS.

4.IT ALLOWS ONE USER AT A TIME 4. IT ALLOWS MORE THAN ONE USER AT A TIME.

5. HIERARCHIAL ARRANGEMENT OF DATA 5. STORES DATA IN THE FORM OF ROWS AND COLUMNS.

19. WRITE EXAMPLE OF EXCEPT IN SQL SERVER…?

Select column1,column2 from Table1 EXCEPT select column1,column2 from Table2.


The number and order of the column must be same.Data types also same or comparable .

20.What is sub query in sql server...?

A subquery is a query that is nested inside a SELECT , INSERT , UPDATE , or DELETE


statement, or inside another subquery.

21.Different TYPES OF SUB QUERY in sql server...?

Single-row subqueries. Multiple-row subqueries. Correlated subqueries.

22.How to get the name from the email address in sql server...?

SELECT LEFT(email address, CHARINDEX('@', email address) - 1)

23.How many times repeated that a letter in a word in sql server...?

len(str) - len(replace(str, 'x', '')).

24.What are the functions in sql server...?

1.Scalar functions:- May or may not have parameters,but always return a single value.The
returned value can be of any data type,except text,ntext,image,cursor,and timestamp.
2.Inline table-valued functions:-
3.Multi-statement table-valued functions.

25.While creating stored procedure what are the functions used in sql server...?

DML functions like insert, update, delete etc.


26. CREATE PROCEDURE PROCEDURENAME
AS
BEGIN
SELECT STATEMENT FROM TABLE NAME

END;

GO

27. What is difference between 'View' and 'Stored Procedure'?


◼ A View is a virtual table that gets data from the base table .It is basically a Select
statement, while Stored Procedure is a set of sql statement stored on database server.

28.What is difference between a Common Table Expression and temporary


table?
◼ CTE is a temporary result set that is defined within execution scope of a
single SELECT ,DELETE,UPDATE statement while temporary table is stored in TempDB
and gets deleted once the session expires.

29.Differentiate between a clustered index and a non-clustered index?


◼ A clustered index determines physical ordering of data in a table and a table can
have only one clustered index while a non-clustered index is analogous to index of a
book where index is stored at one place and data at other place and index will have
pointers to storage location of the data, a table can have more than one non-clustered
index.

30. Explain triggers ?


◼ They are sql codes which are automatically executed in response to certain events
on a table.They are used to maintain integrity of data.
31. HOW TO WRITE A SQL QUERY MAX VALUE IN EACH ROW WITH EXAMPLE…?
SELECT COLUMN1,COLUMN2,COLUMN3, GREATEST(COLUMN1,COLUMN2,COLUMN3) AS MAX_VALUE FROM
YOUR_TABLE;

1. NOT NULL: It cannot have a null


2. UNIQUE : it is unique only but it can have null
3. PRIMARY KEY: it is a unique id and it can never be null
4. FOREIGN KEY: foreign key referencing from the primary key(it allows duplicates
and null values)
5. CHECK: column satisfies a specific condition
6. DEFAULT: default value for a column

HCL is hiring for Power BI Developer/Reporting role.


Please share your updated CV to sadhanakumari.droli@hcl.com

WITH CTE AS (

SELECT column1, column2, column3, ROW_NUMBER() OVER(PARTITION BY column1,


column2, column3 ORDER BY column1, column2, column3) AS RowNumber FROM
table_name)

SELECT * FROM CTE WHERE RowNumber > 1

With CTE as (Select emp_no,emp_name,row_number() Over (partition by emp_no order


by emp_no) as number_of_employ From Employ_DB) Select * from CTE order by
emp_name,emp_no
I want to remove duplicate values based on rows in sql server….
WITH CTE AS (
SELECT *,
ROW_NUMBER() OVER (PARTITION BY Column1, Column2 ... ORDER
BY (SELECT NULL)) AS RowNum
FROM YourTableName
)
DELETE FROM CTE WHERE RowNum > 1;

Write a query to calculate the even and odd records from a Table...?
Select * from tablename where MOD (columnname,2)=0;
Select * from tablename where MOD (columnname,2)=1;
Mod function is used to get the reminder from a division.

Write a query to display the first and the last record from the
table...?
Select * from tablename where columnname=(select min(columnname) from
tablename );
Select * from tablename where columnname=(select max(columnname) from
tablename );

You might also like