You are on page 1of 8

Write a sql query to create a table Persons Column names as follows P_Id int, LastName

varchar(50),FirstName varchar(50), Address varchar(50),City varchar(50)?(All Columns Does


not allow null values)(*3*)
Create table
(
P_Id int NOT NULL ,
LastName varchar(50) NOT NULL,
FirstName varchar(50)NOT NULL,
Address varchar(50) NOT NULL,
City varchar(50) NOT NULL
)
1. Write a sql query to drop a database with name TempDataBase?(Just Write Query no need
to Excecute)(*1*)
DROP TABLE TempDataBase
2. Write a sql query to add column ‘Column1’ to personsTmp in the database TempDataBase?
(*2*)
use TempDataBase
ALTER TABLE personsTmp
ADD Column1 datatype
3. Write a sql query to drop a table with name personsTmp?(*1*)
DROP TABLE personsTmp

4. Get all employee detail from Employees table whose "FirstName" start with latter 'a'?(*1*)
SELECT * FROM [Employees] WHERE FirstName like 'a%'
5. Write SQL statement to selects all customers with Country NOT containing the pattern
"land"?(*1*)
SELECT * FROM Customers WHERE Country NOT LIKE '%land%'
6. Customers is Master table,Orders is Transaction Table, in between there is foreign key
Relationship,Then Write SQL statement to selects Customers with Order Details?(*3*)
SELECT Customers.CustomerName, Orders.OrderID FROM Customers
INNER JOIN Orders ON Customers.CustomerID=Orders.CustomerID
ORDER BY Customers.CustomerName

7. There are two tables one is Suppliers and another one is Customers, write a Query to copy
Customers data to suppliers data where country is Germany ?(*2*)
INSERT INTO Customers (CustomerName, Country) SELECT SupplierName, Country FROM
Suppliers WHERE Country='Germany'
8. Get all employee details from Employees table whose joining date between "2013-01-01"
and "2013-12-01"?(*2*)
SELECT * FROM [Employees] WHERE JoiningDate BETWEEN '2013-01-01' AND '2013-12-01'
9. Get all employee detail from Employees table whose "FirstName" start with any single
character between 'a-p'?(*2*)
SELECT * FROM [Employees] WHERE FirstName like '[a-p]%'

10. Write a SQL query to create a table Persons with primerykey constrain for P_Id Column
names as follows P_Id int, LastName varchar(50),FirstName varchar(50), Address
varchar(50),City varchar(50)?(All Columns Does not allow null values)(*3*)
Create table
(
P_Id int NOT NULL PRIMARY KEY,
LastName varchar(50) NOT NULL,
FirstName varchar(50)NOT NULL,
Address varchar(50) NOT NULL,
City varchar(50) NOT NULL
)
11. Write a sql query to Truncate a table with name personsTmp?(*1*)
truncate table personsTmp
12. Write a sql query to delete‘Column1’ to personsTmp in the database TempDataBase?(*2*)
use TempDataBase
ALTER TABLE personsTmp
DROP COLUMN Column1
13. Write a sql query to delete a table with name personsTmp?(*1*)
delete from personsTmp
14. Create a View to Display first name,Lastname and combination of First and last name and
Gender as M/F.(if male then M, if Female then F) from customers table?(*2*)

SELECT FirstName, CASE WHEN Gender = 'Male' THEN 'M' WHEN Gender = 'Female' THEN 'F'
END AS [Gender] FROM [Employees]

15. Suppose you have one table world(name, continent, area, population, gdp) ,write a query to
add a column with name ‘Longest River’ to this table ?(*3*)

ALTER TABLE world ADD [Longest River] VARCHAR(20) NULL.


16. Write a query To DROP a Foreign KEY Constraint ?
ALTER TABLE Persons
DROP CONSTRAINT pk_PersonID

17. Write a sql query to create a table Persons with primery key constrain for P_Id Column
names as follows P_Id int, LastName varchar(50),FirstName varchar(50), RefID int,
Address varchar(50),City varchar(50) and create a table Orders O_Id int,OrderNo int and
P_Id is Foreign key for this table and also put Foreign key to RefID - P_Id?(*5*)
Create table
(
P_Id int NOT NULL PRIMARY KEY,
LastName varchar(50) NOT NULL,
FirstName varchar(50)NOT NULL,
Address varchar(50) NOT NULL,
City varchar(50) NOT NULL
)
CREATE TABLE Orders
(
O_Id int NOT NULL PRIMARY KEY,
OrderNo int NOT NULL,
P_Id int FOREIGN KEY REFERENCES Persons(P_Id)
)
18. Write a query for combine FirstName and LastName and display it as "Name" (also include
white space between first name & last name)?(*1*)
SELECT FirstName +' '+ LastName AS [Name] FROM [Employees]
19. Find the capital and the name where the capital includes the name of the country?(*3*)
SELECT name,capital FROM world WHERE name LIKE '%'+capital+'%'
20. For every match involving poland, Create a View to show the matchid, date and the number
of goals scored for that team ?(*2*)
CREATE VIEW View1 AS
SELECT matchid,mdate,COUNT(teamid) FROM game
Inner JOIN goal ON matchid = id
WHERE (team1 = 'POL' OR team2 = 'POL')
GROUP BY matchid,mdate

21. Create a View to Show the winners with first name John from the tabel Nobel?(*1*)

CREATE VIEW Winners AS SELECT winner FROM nobel where winner like 'John%'
22. Suppose We have a table game shown below ,Create a View Games1920 which contains all
games after 1920?(*2*)
CREATE VIEW Games1920 AS
SELECT yr,stadium FROM games
WHERE yr>1920;
SELECT * FROM Games1920

23. Write a query To DROP a PRIMARY KEY Constraint ?(*1*)


ALTER TABLE Persons
DROP CONSTRAINT pk_PersonID
24. Write a sql query to create a table Persons with primery key constrain for P_Id Column
names as follows P_Id int, LastName varchar(50),FirstName varchar(50),
Address varchar(50),City varchar(50) and create a table Orders O_Id int,OrderNo int and P_Id
is Foreign key for this table?(*3*)
Create table
(
P_Id int NOT NULL PRIMARY KEY,
LastName varchar(50) NOT NULL,
FirstName varchar(50)NOT NULL,
Address varchar(50) NOT NULL,
City varchar(50) NOT NULL
)
CREATE TABLE Orders
(
O_Id int NOT NULL PRIMARY KEY,
OrderNo int NOT NULL,
P_Id int FOREIGN KEY REFERENCES Persons(P_Id)
)
25. Write SQL statement to selects all customers from Country containing the name "paris and
London"?(*1*)
SELECT * FROM Customers WHERE City IN ('Paris','London')
26. Greece has a double e - who has a double o?(*1*)
SELECT name FROM world WHERE name LIKE '%oo%'.

27. Create a View to List the films after 1962 with year,Title and director Name of films
including 1962?(*2*)
Create View Winners AS SELECT movie.id,yr, title,name FROM movie
inner join actor on actor.id=movie.director
WHERE yr>=1962
28. Write an Sql Query : Create a View to List the films where 'Harrison Ford' has appeared -
but not in the starring role after 1985 [Note: the ord field of casting gives the position of the
actor. If ord=1 then this actor is in the starring role]?(*2*)
CREATE VIEW Games1920 AS
SELECT title FROM movie, casting, actor WHERE name='Harrison Ford' AND movieid=movie.id
AND actorid=actor.id AND ord<>1 and yr>1958
29. What id Cascading update and Cascading delete in sql try to explain how its useful?

Use the ON DELETE CASCADE option if you want rows deleted in the child table when
corresponding rows are deleted in the parent table. If you do not specify cascading
deletes, the default behavior of the database server prevents you from deleting data in a
table if other tables reference it.
If you specify this option, when you delete a row in the parent table, the database server
also deletes any rows associated with that row (foreign keys) in a child table. The
advantage of the ON DELETE CASCADE option is that it allows you to reduce the
quantity of SQL statements needed to perform delete actions.

If you specify this option, when you update a row in the parent table, the database server
also updates any rows associated with that row (foreign keys) in a child table. The
advantage of the ON UPDATE CASCADE option is that it allows you to update child table
when you update parent table.

30. What is main Disadvantage of cascading delete in sql server?


If by mistake you delete a row in the parent table all the rows in the corresponding child tables
will be deleted and it will be PITA to figure out what you deleted.
31. Create a table Country with columns id as int ,name as varchar with length 50.then add check
consrtrain to id which can allow only greater than 10?
CREATE TABLE counrtry
(
Id int NOT NULL CHECK (P_Id>10),
Name varchar(50) NOT NULL,
)

32. Write a query to add check consrtrain to id which can allow only greater than 10?(if table not
Exists Create table counrtry(id int,name varchar(50))
ALTER TABLE counrtry
ADD CONSTRAINT chk_Person CHECK (P_Id>10)

33. Write a query to add check consrtrain to id which can allow only greater than 10?(if table not
Exists Create table counrtry(id int,name varchar(50))
ALTER TABLE Persons
ADD CONSTRAINT chk_Person CHECK (P_Id>10 AND Name ='Sandnes')

34. What is syntax to drop Check Constrain in table?

ALTER TABLE Persons


DROP CONSTRAINT chk_Person

35. What is default constrains in sql server explain?

The DEFAULT constraint is used to insert a default value into a column.


The default value will be added to all new records, if no other value is specified.
36. Create a table Temp with columns id ,name,createddate with datatypes int,varchar(50),datetime
add default constrain to createddate to insert current running date?
CREATE TABLE Temp
(
Id int NOT NULL,
Name intvarchar(50) NOT NULL,
OrderDate date DEFAULT GETDATE()
)
37. What is the sql syntax to add default constrain in for a table?
ALTER TABLE TableName
ALTER COLUMN ColumnName SET DEFAULT 'DefaultValue’
38. Write is syntax to drop Default Constrain in table?

ALTER TABLE Persons


ALTER COLUMN City DROP DEFAULT
39. In a table one column can allow null values,some days later you cannot allow null values to that
column what is the procedure to get above condition?
To Achive the above condition simply add default constrain
ALTER TABLE [Table] ADD CONSTRAINT [Constraint] DEFAULT 0 FOR [Column];
40. In a table one column can allow null values,some days later you cannot allow null values to that
column what is the procedure to get above condition?(Try to avoid Defalult Constrain)
To achieve above condition,
First update All null values to 0
UPDATE [Table] SET [Column]=0 WHERE [Column] IS NULL
Then, update the table definition to disallow NULLs:
ALTER TABLE [Table] ALTER COLUMN [Column] INTEGER NOT NULL
41. How to update value of a default constraint of a column in sql server?
First Delete Existing Constraint and add new constraint
alter table _temp drop constraint Constraintname
alter table _temp add constraint Constraintname default 2 for Column
42. Write a query to get list of existing constraints for a table in sql server?
SELECT * FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS where
TABLE_NAME='Marks'
43. What is the difference between primary key and unique key in sql server explain?
Primary Key enforces uniqueness of the column on which they are defined. Primary Key creates a
clustered index on the column. Primary Key does not allow Nulls.
Unique Key enforces uniqueness of the column on which they are defined. Unique Key creates a non-
clustered index on the column. Unique Key allows only one NULL Value.

You might also like