Skip to content

Commit 2d91654

Browse files
committed
adding sql problem from learn at knowstar
1 parent 7d4931e commit 2d91654

6 files changed

+86
-0
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#https://www.youtube.com/watch?v=Ip6Ty2qmQXg&list=PL2-GO-f-XvjBl5fpzdfYaPW28PwsLzLc4&index=32
2+
#dont have data file
3+
select emp.firstname,emp.lastname,emp.title,,emp.departmentname,emp.phone,emp.startdate,emp.enddate
4+
from dbo.employee emp, dbo.employee emp1
5+
where emp.firstname = emp1.firstname
6+
and emp.lastname = emp1.lastname
7+
and emp.startdate < emp1.startdate
8+
and emp.enddate > emp1.startdate
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
create table survey (
2+
surveyid int,
3+
response varchar(150)
4+
);
5+
#DROP TABLE survey;
6+
select * from survey;
7+
#insert into survey values (1,'ZZZZZXXXXCCCVVVBBNNMMLLKHJFGDFSDASASSAAQWEWERETRTYRYIYYIOIPUT');
8+
#insert into survey values (2,'QWPDHBCNFDHFFGALSDDCS');
9+
10+
SELECT surveyid,response,length(response) , replace(response,'Y',''), length(replace(response,'Y',''))
11+
from survey;
12+
13+
14+
SELECT surveyid,response,length(response) - length(replace(response,'Y','')) as stringoccured
15+
from survey;
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
select * from superstore_orders
2+
3+
#start with A
4+
select Customer_Name from superstore_orders
5+
where Customer_Name like 'A%';
6+
#end with A
7+
select Customer_Name from superstore_orders
8+
where Customer_Name like '%A';
9+
10+
#length with 3 char end last two char end with an,_specify single char
11+
select Customer_Name from superstore_orders
12+
where Customer_Name like '_an';
13+
14+
#start with either a or c or d,SIGLE CHAR CONSTRAINT
15+
select Customer_Name from superstore_orders
16+
where Customer_Name like '[ACD]%';
17+
18+
#start with RANGE
19+
select Product_Name from superstore_orders
20+
where Product_Name like '[A-P]%';
21+
#NOT BETWEEN A AND P
22+
select Product_Name from superstore_orders
23+
where Product_Name like '[^A-P]%';
24+
25+
select ORDER_ID from superstore_orders;
26+
#3RD CHAR WOULD BE HIPHEN
27+
select ORDER_ID from
28+
superstore_orders WHERE ORDER_ID LIKE '__-%';
29+
30+
#
31+
SELECT PRODUCT_NAME from
32+
superstore_orders
33+
WHERE PRODUCT_NAME LIKE '%10[/]%';
34+
35+
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#How to find departments having only male / female employees
2+
# How to find strings with lower case characters | Case Insensitive | Collate
3+
4+
select upper(FirstName) as uper_first,FirstName from dbo.Emp
5+
where upper(FirstName) COLLATE Latin1_Genral_CS_AS != FirstName;
6+
7+
select SERVERPROPERTY('Collation')# CI MEANS CASE SENSITIVE
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#Difference between Count(*), Count(1), Count(colname)
2+
#I have seen/heard select count(*) from tableA returns count of all rows in table regardless of whether a row has all null or some null values,
3+
#on the other hard select count(columnA) from tableA returns the number of records that do not have null in columnA.
4+
#But what if we use a group by? I have seen no info on this. are they equivalent?
5+
select COUNT(*) from dbo.Emp;
6+
select COUNT(1) from dbo.Emp;
7+
#above both are same,in backend same execution plan is genrated for both
8+
9+
#if null is there then it does not count,it will count not null values in that column
10+
select COUNT(lastname) from dbo.Emp;
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#How to find Maximum of multiple columns
2+
#ssmsonly
3+
select category,
4+
select max(saless)
5+
from (values([2015]),([2016]),([2017]),([2018]),([2019]),([2020]) as salestable(sales) ) as maxSales
6+
from saless;
7+
8+
select * from (values (1), (2), (3), (4) ) as tbl(1);
9+
10+
#hardcoded values
11+
select max(A) from (values (1), (2), (3), (4) ) as tbl(1);

0 commit comments

Comments
 (0)