1. MY sql server
-
to use
describe tablewhich is used in mysql if we want to use it in server use
exec sp_help students- students is the table name -
if we want to list all the table names like
show tablesin mysql
select name from sys.tables
for databases name listing doselect name from sys.databases; -
in sql server we dont have the
limitkeyword to limit show the rows so we use
select top 3 dept,count(name) from students
group by dept;
- to rename a column name in sqlserver
exec sp_rename 'students.marks','student_marks','column'; - use of procedure
create procedure sup
as
begin
select * from suppliers;
end
go
or with params
create procedure sup_name @city varchar(50)
as
begin
select * from suppliers where city=@city
end
go
--to run this
exec sup_name @city='surat';
questions
--select max marks from students
select top 1 * from students
order by marks desc;
select * from students where marks=(select max(marks) from students);
-- other method
select * from students
order by marks desc
offset 1 row fetch next 1 row only;
-- show names of students whoes names start with letter s
select * from students where name like 's%';
-- select 2nd higest marks from students
select top 1 * from students where
marks<(select max(marks) from students)
order by marks desc;
-- select nth highest salary from students here 5th highest
select min(marks) as nhigh from (select top 5 name,marks from students order by marks desc) as temp ;
--or
select top 1 * from students where marks in(select top 5 marks from students order by marks desc)
order by marks asc;
--or
select * from students
order by marks desc
offset 4 row fetch next 1 row only;
-- select name from students where the 3rd letter is h for ignoring a letter use underscore
select * from students where name like '__h%';
"The WHERE clause is used to filter individual rows before any grouping happens, while the HAVING clause is used to filter groups after using GROUP BY.
For example, if I want to select only students who scored above 80, I use WHERE.
But if I want to find names of students who appear more than once, I group them by name and use HAVING COUNT(*) > 1."**
Bonus:
You can say:
"
WHEREworks on raw data,HAVINGworks on aggregated data."
That shows you understand when to use which.