04 October 2019

Common SQL Questions

.Write an SQL Query to find first weekday of the month?


SELECT 

DATENAME(dw, DATEADD(dd,  -DATEPART(dd, GETDATE()) + 1, GETDATE())) 

AS FirstDay

-----------------------------------------

A relationship in DBMS is the scenario where two entities are related to each other. In such a scenario, the table consisting of foreign key references to that of a primary key of the other table.


The different types of relationships in DBMS are as follows:
  • One-to-One Relationship –  Used when a single row in Table A is related to a single row in Table B.
  • One-to-Many Relationship – Used when a single row in Table A is related to many rows in table B.
  • Many-to-Many Relationship – Used when many rows in table A can be related to many rows in table B.
  • Self -Referencing Relationship – Used when a record in table A is related to record in same table.
----------------------------------------------------------------------------------------------------------------

A user-defined function is a function written as per the needs of the user by implementing logic. In these kinds of functions the user is not limited to pre-defined functions and simplify the complex code of predefined function by writing simple code. This function returns a scalar value or a table




.

CREATE FUNCTION samplefunc(@num INT)

RETURNS TABLE

AS

RETURN SELECT * FROM customers WHERE CustId=@num


SELECT * FROM samplefunc(10)

--------------------------------------------------------------------------------------------------------------------

Mention the differences between SUBSTR and CHARINDEX in SQL Server.
SUBSTRCHARINDEX 
Used to return a specific portion of the string in a given string
Used to return a character position in a given specified string
Example:SUBSTRING(‘Edureka’,1,4)
Output:Edur
Example:CHARINDEX(‘r’,’Edureka’,1)
Output:4

No comments:

Post a Comment

Resource Governor

Resource Governor  is a feature that you can use to manage SQL Server workload and system resource consumption. Resource Governor enables y...