SQL All Notes
SQL PROFILER - https://vasanthrv.blogspot.com/2022/08/sql-profiler.html
CREATE TABLE Test (
Id int PRIMARY KEY IDENTITY(1,1) NOT NULL,
Name varchar(30) NOT NULL,
City varchar(50) NULL,
)
Insert into Test(Name,City) values('', '')
16:21
update Test set Name = 'Kumar' where id=3
delete from Test Where id=3
CREATE TABLE regions (
region_id INT IDENTITY(1,1) PRIMARY KEY,
region_name VARCHAR (25) DEFAULT NULL
);
DEFAULTconstraint
CREATE TABLE countries (
country_id CHAR (2) PRIMARY KEY,
country_name VARCHAR (40) DEFAULT NULL,
region_id INT NOT NULL
);
FOREIGN KEY (region_id) REFERENCES regions (region_id) ON DELETE CASCADE ON UPDATE CASCADE
16:39
INSERT INTO regions(region_name) VALUES ('Asia');
INSERT INTO regions(region_name) VALUES ('Europe');
INSERT INTO countries(country_id,country_name,region_id) VALUES ('AR','Argentina',2);
INSERT INTO countries(country_id,country_name,region_id) VALUES ('AU','Australia',3);
1h 14m 28s
53m 22s
------------------
CREATE TABLE users (
user_id INT Identity(1,1) PRIMARY KEY,
username VARCHAR(255) NOT NULL,
password VARCHAR(255) NOT NULL,
CONSTRAINT uc_username UNIQUE (username)
);
insert into users values('test1','ttt')
insert into users values('test1','ttt')
CREATE TABLE trainings (
employee_id INT Identity(1,1) PRIMARY KEY,
course_id INT,
taken_date DATE NOT NULL,
);
INSERT INTO trainings(employee_id,course_id)
VALUES(1,1); ---error
INSERT INTO trainings(employee_id,course_id,taken_date)
VALUES(1,1,GETDATE());
CREATE TABLE products (
product_id INT PRIMARY KEY,
product_name VARCHAR(255) NOT NULL,
selling_price int CHECK (selling_price > 0)
);
insert into products values(1,'test',0)
insert into products values(1,'test',50)
CREATE TABLE Orders (
ID int NOT NULL,
OrderNumber int NOT NULL,
OrderDate date DEFAULT GETDATE()
);
insert into Orders(ID,OrderNumber ) Values(1,1)
SELECT REVERSE('abcd')
SELECT UPPER('Allah-hus-samad');
select UPPER(first_name) from employees
SELECT ROUND(157.96,0)
SELECT GETDATE()
SELECT DAY(GETDATE())
SELECT FORMAT (getdate(), 'dd-MM-yyyy') as date
Date Parts: can use the name or listed abbreviations:
year, yy, yyyy
quarter, qq, q
month, mm, m
dayofyear, dy, y*
day, dd, d*
weekday, dw, w*
week, wk, ww
hour, hh
minute, mi, n
second, ss, s
millisecond, ms
microsecond, mcs
nanosecond, ns
select convert(varchar, getdate(), 1)
-----------------------
CREATE TABLE regions (
region_id INT IDENTITY(1,1) PRIMARY KEY,
region_name VARCHAR (25) DEFAULT NULL
);
CREATE TABLE countries (
country_id CHAR (2) PRIMARY KEY,
country_name VARCHAR (40) DEFAULT NULL,
region_id INT NOT NULL,
FOREIGN KEY (region_id) REFERENCES regions (region_id) ON DELETE CASCADE ON UPDATE CASCADE
);
CREATE TABLE locations(
location_id INT IDENTITY(1,1) PRIMARY KEY,
street_address VARCHAR (40) DEFAULT NULL,
postal_code VARCHAR (12) DEFAULT NULL,
city VARCHAR (30) NOT NULL,
state_province VARCHAR (25) DEFAULT NULL,
country_id CHAR (2) NOT NULL,
FOREIGN KEY (country_id) REFERENCES countries (country_id) ON DELETE CASCADE ON UPDATE CASCADE
);
CREATE TABLE jobs (
job_id INT IDENTITY(1,1) PRIMARY KEY,
job_title VARCHAR (35) NOT NULL,
min_salary DECIMAL (8, 2) DEFAULT NULL,
max_salary DECIMAL (8, 2) DEFAULT NULL
);
CREATE TABLE departments (
department_id INT IDENTITY(1,1) PRIMARY KEY,
department_name VARCHAR (30) NOT NULL,
location_id INT DEFAULT NULL,
FOREIGN KEY (location_id) REFERENCES locations (location_id) ON DELETE CASCADE ON UPDATE CASCADE
);
CREATE TABLE employees (
employee_id INT IDENTITY(1,1) PRIMARY KEY,
first_name VARCHAR (20) DEFAULT NULL,
last_name VARCHAR (25) NOT NULL,
email VARCHAR (100) NOT NULL,
phone_number VARCHAR (20) DEFAULT NULL,
hire_date DATE NOT NULL,
job_id INT NOT NULL,
salary DECIMAL (8, 2) NOT NULL,
manager_id INT DEFAULT NULL,
department_id INT DEFAULT NULL,
FOREIGN KEY (job_id) REFERENCES jobs (job_id) ON DELETE CASCADE ON UPDATE CASCADE,
FOREIGN KEY (department_id) REFERENCES departments (department_id) ON DELETE CASCADE ON UPDATE CASCADE,
);
INSERT INTO regions(region_name) VALUES ('Europe');
INSERT INTO regions(region_name) VALUES ('Americas');
INSERT INTO regions(region_name) VALUES ('Asia');
INSERT INTO regions(region_name) VALUES ('Middle East and Africa');
INSERT INTO countries(country_id,country_name,region_id) VALUES ('AR','Argentina',2);
INSERT INTO countries(country_id,country_name,region_id) VALUES ('AU','Australia',3);
INSERT INTO countries(country_id,country_name,region_id) VALUES ('BE','Belgium',1);
INSERT INTO countries(country_id,country_name,region_id) VALUES ('BR','Brazil',2);
INSERT INTO countries(country_id,country_name,region_id) VALUES ('CA','Canada',2);
INSERT INTO countries(country_id,country_name,region_id) VALUES ('CH','Switzerland',1);
INSERT INTO countries(country_id,country_name,region_id) VALUES ('CN','China',3);
INSERT INTO countries(country_id,country_name,region_id) VALUES ('DE','Germany',1);
INSERT INTO countries(country_id,country_name,region_id) VALUES ('DK','Denmark',1);
INSERT INTO countries(country_id,country_name,region_id) VALUES ('EG','Egypt',4);
INSERT INTO countries(country_id,country_name,region_id) VALUES ('FR','France',1);
INSERT INTO countries(country_id,country_name,region_id) VALUES ('HK','HongKong',3);
INSERT INTO countries(country_id,country_name,region_id) VALUES ('IL','Israel',4);
INSERT INTO countries(country_id,country_name,region_id) VALUES ('IN','India',3);
INSERT INTO countries(country_id,country_name,region_id) VALUES ('IT','Italy',1);
INSERT INTO countries(country_id,country_name,region_id) VALUES ('JP','Japan',3);
INSERT INTO countries(country_id,country_name,region_id) VALUES ('US','US',4);
INSERT INTO countries(country_id,country_name,region_id) VALUES ('UK','UK',4);
INSERT INTO countries(country_id,country_name,region_id) VALUES ('DE','DE',4);
INSERT INTO locations(street_address,postal_code,city,state_province,country_id) VALUES ('2014 Jabberwocky Rd','26192','Southlake','Texas','US');
INSERT INTO locations(street_address,postal_code,city,state_province,country_id) VALUES ('2011 Interiors Blvd','99236','South San Francisco','California','US');
INSERT INTO locations(street_address,postal_code,city,state_province,country_id) VALUES ('2004 Charade Rd','98199','Seattle','Washington','US');
INSERT INTO locations(street_address,postal_code,city,state_province,country_id) VALUES ('147 Spadina Ave','M5V 2L7','Toronto','Ontario','CA');
INSERT INTO locations(street_address,postal_code,city,state_province,country_id) VALUES ('2004 Charade Rd','98199','Seattle','Washington','US');
INSERT INTO locations(street_address,postal_code,city,state_province,country_id) VALUES ('147 Spadina Ave','M5V 2L7','Toronto','Ontario','CA');
INSERT INTO locations(street_address,postal_code,city,state_province,country_id) VALUES ('8204 Arthur St',NULL,'London',NULL,'UK');
INSERT INTO locations(street_address,postal_code,city,state_province,country_id) VALUES ('Magdalen Centre, The Oxford Science Park','OX9 9ZB','Oxford','Oxford','UK');
INSERT INTO locations(street_address,postal_code,city,state_province,country_id) VALUES ('Schwanthalerstr. 7031','80925','Munich','Bavaria','DE');
INSERT INTO jobs(job_title,min_salary,max_salary) VALUES ('Public Accountant',4200.00,9000.00);
INSERT INTO jobs(job_title,min_salary,max_salary) VALUES ('Accounting Manager',8200.00,16000.00);
INSERT INTO jobs(job_title,min_salary,max_salary) VALUES ('Administration Assistant',3000.00,6000.00);
INSERT INTO jobs(job_title,min_salary,max_salary) VALUES ('President',20000.00,40000.00);
INSERT INTO jobs(job_title,min_salary,max_salary) VALUES ('Administration Vice President',15000.00,30000.00);
INSERT INTO jobs(job_title,min_salary,max_salary) VALUES ('Accountant',4200.00,9000.00);
INSERT INTO jobs(job_title,min_salary,max_salary) VALUES ('Finance Manager',8200.00,16000.00);
INSERT INTO jobs(job_title,min_salary,max_salary) VALUES ('Human Resources Representative',4000.00,9000.00);
INSERT INTO jobs(job_title,min_salary,max_salary) VALUES ('Programmer',4000.00,10000.00);
INSERT INTO jobs(job_title,min_salary,max_salary) VALUES ('Marketing Manager',9000.00,15000.00);
INSERT INTO departments(department_name,location_id) VALUES ('Administration',9);
INSERT INTO departments(department_name,location_id) VALUES ('Marketing',8);
INSERT INTO departments(department_name,location_id) VALUES ('Purchasing',7);
INSERT INTO departments(department_name,location_id) VALUES ('Human Resources',5);
INSERT INTO departments(department_name,location_id) VALUES ('Shipping',1);
INSERT INTO departments(department_name,location_id) VALUES ('IT',2);
INSERT INTO departments(department_name,location_id) VALUES ('Public Relations',4);
INSERT INTO departments(department_name,location_id) VALUES ('Sales',4);
INSERT INTO departments(department_name,location_id) VALUES ('Executive',7);
INSERT INTO departments(department_name,location_id) VALUES ('Finance',9);
INSERT INTO departments(department_name,location_id) VALUES ('Accounting',9);
INSERT INTO employees(first_name,last_name,email,phone_number,hire_date,job_id,salary,manager_id,department_id) VALUES ('steven','king','steven.king@sqltutorial.org','515.123.4567','1987-06-17',4,24000.00,NULL,24);
INSERT INTO employees(first_name,last_name,email,phone_number,hire_date,job_id,salary,manager_id,department_id) VALUES ('neena','kochhar','neena.kochhar@sqltutorial.org','515.123.4568','1989-09-21',5,17000.00,100,25);
INSERT INTO employees(first_name,last_name,email,phone_number,hire_date,job_id,salary,manager_id,department_id) VALUES ('Lex','De Haan','lex.de haan@sqltutorial.org','515.123.4569','1993-01-13',5,17000.00,100,26);
INSERT INTO employees(first_name,last_name,email,phone_number,hire_date,job_id,salary,manager_id,department_id) VALUES ('alexander','hunold','alexander.hunold@sqltutorial.org','590.423.4567','1990-01-03',9,9000.00,102,25);
INSERT INTO employees(first_name,last_name,email,phone_number,hire_date,job_id,salary,manager_id,department_id) VALUES ('bruce','ernst','bruce.ernst@sqltutorial.org','590.423.4568','1991-05-21',9,6000.00,103,24);
INSERT INTO employees(first_name,last_name,email,phone_number,hire_date,job_id,salary,manager_id,department_id) VALUES ('david','austin','david.austin@sqltutorial.org','590.423.4569','1997-06-25',9,4800.00,103,26);
select * from departments
select * from employees
INSERT INTO employees(first_name,last_name,email,phone_number,hire_date,job_id,salary,manager_id,department_id) VALUES ('steven','king','steven.king@sqltutorial.org','515.123.4567','1987-06-17',4,24000.00,NULL,8);
INSERT INTO employees(first_name,last_name,email,phone_number,hire_date,job_id,salary,manager_id,department_id) VALUES ('neena','kochhar','neena.kochhar@sqltutorial.org','515.123.4568','1989-09-21',5,17000.00,100,9);
INSERT INTO employees(first_name,last_name,email,phone_number,hire_date,job_id,salary,manager_id,department_id) VALUES ('Lex','De Haan','lex.de haan@sqltutorial.org','515.123.4569','1993-01-13',5,17000.00,100,8);
INSERT INTO employees(first_name,last_name,email,phone_number,hire_date,job_id,salary,manager_id,department_id) VALUES ('alexander','hunold','alexander.hunold@sqltutorial.org','590.423.4567','1990-01-03',9,9000.00,102,9);
INSERT INTO employees(first_name,last_name,email,phone_number,hire_date,job_id,salary,manager_id,department_id) VALUES ('bruce','ernst','bruce.ernst@sqltutorial.org','590.423.4568','1991-05-21',9,6000.00,103,10);
INSERT INTO employees(first_name,last_name,email,phone_number,hire_date,job_id,salary,manager_id,department_id) VALUES ('david','austin','david.austin@sqltutorial.org','590.423.4569','1997-06-25',9,4800.00,103,11);
53m 9s
57m 30s
08:39
SELECT
employee_id,
first_name,
last_name,
hire_date,
salary
FROM
employees
ORDER BY
first_name,
last_name DESC;
--- last_name DESC;
--------------------------------
SELECT
DISTINCT salary
FROM
employees
--------------------------------
SELECT
employee_id,
first_name,
last_name,
salary
FROM employees1
ORDER BY
salary DESC
OFFSET 2 ROWS
FETCH NEXT 2 ROWS ONLY;
--------------------------------
08:53
SELECT
employee_id,
first_name,
last_name,
salary
FROM
employees
WHERE
salary > 14000
ORDER BY
salary DESC;
salary <> 17000
salary != 17000
--------------------------------
sELECT
*
FROM
employees
WHERE
salary < 17000 and department_id = 4
--------------------------------
SELECT
employee_id,
first_name,
last_name,
salary
FROM
employees
WHERE
salary BETWEEN 2000 AND 8000
--------------------------------
SELECT
employee_id,
first_name,
last_name,
job_id
FROM
employees
WHERE
department_id IN ( 9, 10,11)
--------------------------------
SELECT
employee_id,
first_name,
last_name
FROM
employees
WHERE
first_name LIKE 'Da%';
--------------------------------
SELECT
*
FROM
employees
WHERE
manager_id IS NULL;
--------------------------------
SELECT
*
FROM
employees
WHERE
manager_id IS NOT NULL;
--------------------------------
SELECT
employee_id,
first_name,
last_name,
department_id
FROM
employees
WHERE
department_id NOT IN (8, 9)
--------------------------------
select YEAR(hire_date) from employees
09:25
SELECT
first_name,
last_name,
hire_date,
CASE (2022 - YEAR(hire_date))
WHEN 20 THEN '20 years'
WHEN 25 THEN '25 years'
WHEN 26 THEN '26 years'
WHEN 27 THEN '27 years'
WHEN 28 THEN '28 years'
WHEN 29 THEN '29 years'
WHEN 30 THEN '30 years'
WHEN 31 THEN '31 years'
WHEN 32 THEN '32 years'
WHEN 33 THEN '33 years'
WHEN 34 THEN '34 years'
WHEN 35 THEN '35 years'
WHEN 36 THEN '36 years'
END aniversary
FROM
employees1
--------------------------------
DATE ONLY FORMATS | |||
Format # | Query | Format | Sample |
---|---|---|---|
1 | select convert(varchar, getdate(), 1) | mm/dd/yy | 12/30/22 |
2 | select convert(varchar, getdate(), 2) | yy.mm.dd | 22.12.30 |
3 | select convert(varchar, getdate(), 3) | dd/mm/yy | 30/12/22 |
4 | select convert(varchar, getdate(), 4) | dd.mm.yy | 30.12.22 |
5 | select convert(varchar, getdate(), 5) | dd-mm-yy | 30-12-22 |
6 | select convert(varchar, getdate(), 6) | dd-Mon-yy | 30 Dec 22 |
7 | select convert(varchar, getdate(), 7) | Mon dd, yy | Dec 30, 22 |
10 | select convert(varchar, getdate(), 10) | mm-dd-yy | 12-30-22 |
11 | select convert(varchar, getdate(), 11) | yy/mm/dd | 22/12/30 |
12 | select convert(varchar, getdate(), 12) | yymmdd | 221230 |
23 | select convert(varchar, getdate(), 23) | yyyy-mm-dd | 2022-12-30 |
31 | select convert(varchar, getdate(), 31) | yyyy-dd-mm | 2022-30-12 |
32 | select convert(varchar, getdate(), 32) | mm-dd-yyyy | 12-30-2022 |
33 | select convert(varchar, getdate(), 33) | mm-yyyy-dd | 12-2022-30 |
34 | select convert(varchar, getdate(), 34) | dd-mm-yyyy | 30-12-2022 |
35 | select convert(varchar, getdate(), 35) | dd-yyyy-mm | 30-2022-12 |
101 | select convert(varchar, getdate(), 101) | mm/dd/yyyy | 12/30/2022 |
102 | select convert(varchar, getdate(), 102) | yyyy.mm.dd | 2022.12.30 |
103 | select convert(varchar, getdate(), 103) | dd/mm/yyyy | 30/12/2022 |
104 | select convert(varchar, getdate(), 104) | dd.mm.yyyy | 30.12.2022 |
105 | select convert(varchar, getdate(), 105) | dd-mm-yyyy | 30-12-2022 |
106 | select convert(varchar, getdate(), 106) | dd Mon yyyy | 30 Dec 2022 |
107 | select convert(varchar, getdate(), 107) | Mon dd, yyyy | Dec 30, 2022 |
110 | select convert(varchar, getdate(), 110) | mm-dd-yyyy | 12-30-2022 |
111 | select convert(varchar, getdate(), 111) | yyyy/mm/dd | 2022/12/30 |
112 | select convert(varchar, getdate(), 112) | yyyymmdd | 20221230 |
TIME ONLY FORMATS | |||
8 | select convert(varchar, getdate(), 8) | hh:mm:ss | 00:38:54 |
14 | select convert(varchar, getdate(), 14) | hh:mm:ss:nnn | 00:38:54:840 |
24 | select convert(varchar, getdate(), 24) | hh:mm:ss | 00:38:54 |
108 | select convert(varchar, getdate(), 108) | hh:mm:ss | 00:38:54 |
114 | select convert(varchar, getdate(), 114) | hh:mm:ss:nnn | 00:38:54:840 |
DATE & TIME FORMATS | |||
0 | select convert(varchar, getdate(), 0) | Mon dd yyyy hh:mm AM/PM | Dec 30 2022 12:38AM |
9 | select convert(varchar, getdate(), 9) | Mon dd yyyy hh:mm:ss:nnn AM/PM | Dec 30 2022 12:38:54:840AM |
13 | select convert(varchar, getdate(), 13) | dd Mon yyyy hh:mm:ss:nnn AM/PM | 30 Dec 2022 00:38:54:840AM |
20 | select convert(varchar, getdate(), 20) | yyyy-mm-dd hh:mm:ss | 2022-12-30 00:38:54 |
21 | select convert(varchar, getdate(), 21) | yyyy-mm-dd hh:mm:ss:nnn | 2022-12-30 00:38:54.840 |
22 | select convert(varchar, getdate(), 22) | mm/dd/yy hh:mm:ss AM/PM | 12/30/22 12:38:54 AM |
25 | select convert(varchar, getdate(), 25) | yyyy-mm-dd hh:mm:ss:nnn | 2022-12-30 00:38:54.840 |
26 | select convert(varchar, getdate(), 26) | yyyy-dd-mm hh:mm:ss:nnn | 2022-30-12 00:38:54.840 |
27 | select convert(varchar, getdate(), 27) | mm-dd-yyyy hh:mm:ss:nnn | 12-30-2022 00:38:54.840 |
28 | select convert(varchar, getdate(), 28) | mm-yyyy-dd hh:mm:ss:nnn | 12-2022-30 00:38:54.840 |
29 | select convert(varchar, getdate(), 29) | dd-mm-yyyy hh:mm:ss:nnn | 30-12-2022 00:38:54.840 |
30 | select convert(varchar, getdate(), 30) | dd-yyyy-mm hh:mm:ss:nnn | 30-2022-12 00:38:54.840 |
100 | select convert(varchar, getdate(), 100) | Mon dd yyyy hh:mm AM/PM | Dec 30 2022 12:38AM |
109 | select convert(varchar, getdate(), 109) | Mon dd yyyy hh:mm:ss:nnn AM/PM | Dec 30 2022 12:38:54:840AM |
113 | select convert(varchar, getdate(), 113) | dd Mon yyyy hh:mm:ss:nnn | 30 Dec 2022 00:38:54:840 |
120 | select convert(varchar, getdate(), 120) | yyyy-mm-dd hh:mm:ss | 2022-12-30 00:38:54 |
121 | select convert(varchar, getdate(), 121) | yyyy-mm-dd hh:mm:ss:nnn | 2022-12-30 00:38:54.840 |
126 | select convert(varchar, getdate(), 126) | yyyy-mm-dd T hh:mm:ss:nnn | 2022-12-30T00:38:54.840 |
127 | select convert(varchar, getdate(), 127) | yyyy-mm-dd T hh:mm:ss:nnn | 2022-12-30T00:38:54.840 |
--------------------------------
(@qty int , @price int, @discount int)
returns int
as
begin
return @qty * (@price - @discount)
end
Comments
Post a Comment