Advertisement

Advertisement

SQL Interview

0. How to rename column name in Sql table  ?

EXEC sp_rename 'Users.PANNumber', 'PanNumber', 'COLUMN';

database  table k column name ko rename krne k liye only upper diye gaye query ko chala de 

(e.g., PanNumber property se PANNumber column)

0. Can we disable a trigger? If yes, how?

Yes, 

0.1. What is SCOPE_IDENTITY() ? 

Ans - It is an inbuilt function, SCOPE_IDENTITY() ki madad se latest inserted row ka ID asani se mil jata hai.

What-is-scopeidentity-and-why-we-use.html

1.Write an SQL query to find the names of employees starting with ‘A’. 


SELECT * FROM Employees WHERE EmpName like 'A%' ;


38. What is the On Delete cascade constraint?

An ‘ON DELETE CASCADE’ constraint is used in MySQL to delete the rows from the child table automatically 

when the rows from the parent table are deleted.

This is particularly useful for maintaining referential integrity between two tables that are related via a foreign key.


1. Difference b/w char and varchar2?

(CHAR space v leleta but varchar2 space nhi leta h)


CHAR always takes the full size you specify, even if the actual data is shorter.

VARCHAR2 only takes as much space as the actual data, no extra space is used.

Example:

If you create a column with CHAR(10) and another with VARCHAR2(10):


If you store 'SQL':

CHAR(10) will store it as 'SQL ' (with 7 spaces).

VARCHAR2(10) will store it as 'SQL' (just 3 characters, no spaces).

So, CHAR wastes space if your data is shorter, while VARCHAR2 only uses the necessary space.


2. What is a Default constraint?

The DEFAULT constraint is used to fill a column with default and fixed values. 

The value will be added to all new records when no other value is provided.


3. What is a Constraint? 

(DPUNFC)

NOT NULL: No empty values allowed.

UNIQUE: All values must be unique.

PRIMARY KEY: Combines NOT NULL and UNIQUE to uniquely identify each row.

FOREIGN KEY: Ensures referential integrity between two tables.

CHECK: Validates data against a specific condition.

DEFAULT: Automatically assigns a value if no value is provided.


Constraints are the rules that we can apply to the type of data in a table. 

That is, we can specify the limit on the type of data that can be stored in a particular column in a table using constraints.


4. What is Clause?


In SQL, a clause is a component of a SQL statement that specifies a particular condition, operation, or filtering criterion. Clauses help define what actions to perform on the data or how to filter and organize the results returned by a query.


Common SQL Clauses:

SELECT Clause:


Specifies the columns to be returned in the result set.

Example:


SELECT EmpID, EmpName FROM Employees;

FROM Clause:


Specifies the table or tables from which to retrieve the data.

Example:


SELECT EmpName FROM Employees;

WHERE Clause:


Filters the records based on a specified condition. Only rows that satisfy the condition will be returned.

Example:


SELECT * FROM Employees WHERE Age > 30;

ORDER BY Clause:


Sorts the result set based on one or more columns. You can specify ascending (ASC) or descending (DESC) order.

Example:


SELECT * FROM Employees ORDER BY EmpName ASC;

GROUP BY Clause:


Groups rows that have the same values in specified columns into summary rows, like calculating aggregates.

Example:


SELECT Department, COUNT(*) FROM Employees GROUP BY Department;

HAVING Clause:


Similar to the WHERE clause but is used for filtering groups created by the GROUP BY clause. It is applied after the grouping has occurred.

Example:


SELECT Department, COUNT(*) 

FROM Employees 

GROUP BY Department 

HAVING COUNT(*) > 5;

JOIN Clause:


Combines rows from two or more tables based on a related column between them.

Example:


SELECT Employees.EmpName, Departments.DeptName 

FROM Employees 

JOIN Departments ON Employees.DeptID = Departments.DeptID;

LIMIT / OFFSET Clause (in some SQL dialects):


Restricts the number of records returned by a query.

Example:


SELECT * FROM Employees LIMIT 10;  -- Returns the first 10 records

Summary:

Clause: A part of a SQL statement that specifies what action to take, how to filter or sort the data, or how to group it.

Common clauses include SELECT, FROM, WHERE, ORDER BY, GROUP BY, HAVING, and JOIN.

Clauses help define the logic of your SQL queries, making them more precise and effective in retrieving the desired data from a database.

SQL Interview SQL Interview Reviewed by Rikesh on October 07, 2024 Rating: 5

No comments:

Powered by Blogger.