In my previous articles I have given the different SQL queries as well as real life SQL scenarios for interview. In this article I would like to give multiple SQL queries or SQL cheat sheet with examples. I would like to give one SQL Cheat Sheet for free download in PDF format so that you can utilize that SQL Cheat Sheet Easily. This cheat sheet you can easily use in your student journey or professional journey too. You can say that All SQL queries and its syntax on single page.
Query 1 : Fetch the records from Column1,Column2 from Employee Table.
SELECT Column1,Column2 FROM Employee;
Query 2 : Fetch all the records from Employee table.
SELECT * FROM Employee
;
Query 3 : What is Query data and filter rows with a condition
SELECT Column1,Column2
FROM Employee
WHERE condition;
Query 4 : What is Query distinct rows from a table
SELECT DISTINCT Column1 FROM Employee
WHERE condition;
Query 5 : How to Sort the result set in ascending or descending order
SELECT Column1,Column2
FROM Employee
ORDER BY Column1 ASC [DESC];
Query 6 : How to return N rows from Employee table.
SELECT Column1,Column2 FROM Employee
ORDER BY Column1
LIMIT n OFFSET offset;
Query 7 : How to Group rows using an aggregate function?
SELECT Column1, aggregate(Column2)
FROM Employee
GROUP BY Column1;
Query 8 : How do you filter groups using Having Clause.
SELECT Column1, aggregate(Column2) FROM Employee GROUP BY Column1 HAVING condition;
Query 1 : How to Join 2 tables using Inner join table – t1 and table t2
SELECT Column1, Column2 FROM t1 INNER JOIN t2 ON condition;
Query 2 : How to Join 2 tables using left join – table-t1 and table t2.
SELECT Column1, Column2
FROM t1
LEFT JOIN t2 ON condition;
Query 3 : How to Join 2 tables using right join – table-t1 and table t2.
SELECT Column1, Column2
FROM t1
RIGHT JOIN t2 ON condition;
Query 4 : How to Perform full outer join with using t1 and t2 table.
SELECT Column1, Column2
FROM t1
FULL OUTER JOIN t2 ON condition;
Query 5 : How to produce Cartesian product of 2 tables?
SELECT Column1, Column2
FROM t1
CROSS JOIN t2;
Way 2 for cartesian or cross join :
SELECT Column1, Column2
FROM t1, t2;
Query 6 :How to do Self Join of the table t1
SELECT Column1, Column2
FROM t1 A
INNER JOIN t1 B ON condition;
Query 1 : How to Combine two rows from table t1 and table t2.
SELECT Column1,Column2 FROM t1
UNION [ALL]
SELECT Column1,Column2
FROM t2;
Query 2 : How to intersect the data from table t1 and table t2.
SELECT Column1,Column2
FROM t1
INTERSECT
SELECT Column1,Column2
FROM t2;
Query 3 : How to Subtract a result set of table t1 from another result set of table t2.
SELECT Column1,Column2
FROM t1
MINUS
SELECT Column1,Column2
FROM t2;
Query 4 : How to Query rows using pattern matching %, _
SELECT Column1,Column2
FROM t1
WHERE Column1
[NOT] LIKE pattern
;
Query 5: How to fetch records from Value list?
SELECT Column1,Column2
FROM t
WHERE Column1
[NOT] IN value_list;
Query 6 : How to fetch all columns between two ranges?
SELECT
1FROM t WHERE
Column1,Column2
Column
BETWEEN Low_range_value AND high_range_value;
Query 7: How to Check if values in a table is NULL or not?
SELECT Column1,Column2
FROM t
WHERE Column1
IS [NOT] NULL;
Query 1 : Create a new table t1 with three columns and having primary key.
CREATE TABLE t (
id INT PRIMARY KEY,
name VARCHAR NOT NULL,
price INT DEFAULT 0
);
Query 2 : How to delete the table from the database?
DROP TABLE t ;
Query 3 : How to add a new column to the table t1?
ALTER TABLE t ADD column;
Query 4 : How to Drop column column1 from the table?
ALTER TABLE t DROP COLUMN column1 ;
Query 5 : How to Add a constraint to the table?
ALTER TABLE t ADD constraint;
Query 6 : How to Drop a constraint from table t1?
ALTER TABLE t DROP constraint;
Query 7 : How to rename a table from t1 to t2?
ALTER TABLE t1 RENAME TO t2;
Query 8 :How to rename column c1 to c2?
ALTER TABLE t1 RENAME c1 TO c2 ;
Query 9 : How to remove data from table t1?
TRUNCATE TABLE t;
Query 1 : How to set two primary keys (Composite key) for one table t1 ?
CREATE TABLE t1(
column1 INT, column2 INT, column3 VARCHAR,
PRIMARY KEY (column1,column2)
);
Query 2 : How to Set Foreign Key to table t1?
CREATE TABLE t1(
column1
INT PRIMARY KEY,
column2
INT,
FOREIGN KEY (column2
) REFERENCES t2(column2
)
);
Query 3 :How to set unique columns for table t1?
CREATE TABLE t1(
column1
INT, column2
INT,column3 INT
UNIQUE(column2
,column3
)
);
Query 4 : How to add Check constraint which ensure column1 > 0 and values in column1 >= column2?
CREATE TABLE t1( column1 INT, column2 INT, CHECK(
2column1
> 0 ANDcolumn1
>=column
) );
Query 5 : How to set Not Null Constraint for table t1?
CREATE TABLE t1(
column1
INT PRIMARY KEY,
column2
VARCHAR NOT NULL
);
Query 1 :How to insert one row into a table t1?
INSERT INTO t(column_list)
VALUES(value_list);
commit;
Query 2 : How to Insert multiple rows in table t1?
INSERT INTO t1(column_list)
VALUES (value_list),
(value_list), …;
Query 3 : How to Insert rows from One table (t2) to other table t1?
INSERT INTO t1(column_list)
SELECT column_list
FROM t2;
Query 4 : How to Update new value in the column column1 for all rows?
UPDATE t1
SET column1 = new_value;
Query 5 : How to Update values in the column column1, column2 that match the condition?
UPDATE t
SET column1 = new_value,
column2 = new_value
WHERE condition;
Query 6 : How to Delete all data from the table?
DELETE FROM t1;
Query 7 : How to Delete subset of rows in a table?
DELETE FROM t1
WHERE condition;
Query 1 : How to Create a new view that consists of column1 and column2?
CREATE VIEW v(column1,column2)
AS
SELECT column1
, column2
FROM t1;
Query 2 : How to Create a new view with check constraint?
CREATE VIEW v(
2column1
,column
2) AS SELECTcolumn1
,column
FROM t; WITH [CASCADED | LOCAL] CHECK OPTION;
Query 3 : How to Create a recursive view with multiple tables?
CREATE RECURSIVE VIEW v
AS
select-statement -- anchor part
UNION [ALL]
select-statement; -- recursive part
Query 4 :How to create temporary view
CREATE TEMPORARY VIEW v
AS
SELECT column1, column2
FROM t1;
Query 5 : How to Delete a view
DROP VIEW view_name;
Query 1 : How to Create an index on column1
and column
2 of the t table?
CREATE INDEX index_name
ON t1(column1,column2);
Query 2 : How to Create an unique index on column3, column4 of the t table
CREATE UNIQUE INDEX index_name
ON t(c3,c4)
;
Query 3 : How to Drop Index?
DROP INDEX index_name
;
Query 1 : How to create or Modify the Triggers?
CREATE OR MODIFY TRIGGER trigger_name
WHEN EVENT
ON table_name TRIGGER_TYPE
EXECUTE stored_procedure;
WHEN
EVENT
TRIGGER_TYPE
Query 2 : How to Delete a specific trigger?
DROP TRIGGER trigger_name;
These are some important SQL Cheat sheet with using which team can easily work on SQL queries.
In my previous article I have given details about application support engineer day to day…
In my previous articles I have given the roles and responsibilities of L1,L2 and L3…
In my previous articles i have given the hierarchy of production support in real company…
In this article i would like to provide information about production support organization structure or…
In my previous article I have given roles for L1 and L2 support engineer with…
I have started this new series of how to become application support engineer. This article…
View Comments
Thanks for wonderful collection of all the technical details, this helps in recalling and refreshing some of the DB stuff.
Thanks for good words