Queries for NOLOCK Table Hint


1. Complex Reporting with Aggregations and Joins

SQL Server Query 1

            
 SELECT  
c.CategoryName,
SUM(od.Quantity * od.UnitPrice * (1 - od.Discount)) AS TotalSalesValue
FROM Categories c WITH (NOLOCK)
JOIN Products p WITH (NOLOCK) ON c.CategoryID = p.CategoryID
JOIN [Order Details] od WITH (NOLOCK) ON p.ProductID = od.ProductID
JOIN Orders o WITH (NOLOCK) ON od.OrderID = o.OrderID
GROUP BY c.CategoryName
ORDER BY TotalSalesValue DESC;

Create SQL query with SqlQueryBuilder 1

            
 var (sql1, parameters1) = new SqlQueryBuilder()  
.Select()
.Column("c.CategoryName")
.Column(new SUM(new ColumnArithmatic("od.Quantity").MULTIPLY("od.UnitPrice").MULTIPLY()
.StartBracket(1).SUBTRACT("od.Discount").EndBracket()), "TotalSalesValue")
.From("Categories", "c", new List<IHint>() { new NOLOCK() })
.Join(new List<IJoin>()
{
new INNERJOIN().TableName("Products","p", new List<IHint>() { new NOLOCK() })
.On(new Column("c.CategoryID").Equale(new Column("p.CategoryID"))),
new INNERJOIN().TableName("[Order Details]", "od", new List<IHint>() { new NOLOCK() })
.On(new Column("p.ProductID").Equale(new Column("od.ProductID"))),
new INNERJOIN().TableName("Orders", "o", new List<IHint>() { new NOLOCK() })
.On(new Column("od.OrderID").Equale(new Column("o.OrderID")))
})
.GroupBy(new GroupBy("c.CategoryName"))
.OrderBy(new OrderBy().SetColumnDescending("TotalSalesValue"))
.Build();

Query build by SqlQueryBuilder 1

            
SELECT c.CategoryName,
       SUM(od.Quantity * od.UnitPrice * (@pMAIN_2509031324227604000 - od.Discount)) AS TotalSalesValue
FROM Categories AS c WITH (NOLOCK)
     INNER JOIN
     Products AS p WITH (NOLOCK)
     ON c.CategoryID = p.CategoryID
     INNER JOIN
     [Order Details] AS od WITH (NOLOCK)
     ON p.ProductID = od.ProductID
     INNER JOIN
     Orders AS o WITH (NOLOCK)
     ON od.OrderID = o.OrderID
GROUP BY c.CategoryName
ORDER BY TotalSalesValue DESC;


            
        

Parameters (If used)

Name Value
@pMAIN_2509031324227604000 1

Query Results 1:

  CategoryName TotalSalesValue
1 Beverages 267868.180522919
2 Dairy Products 234507.285217285
3 Confections 167357.224831581
4 Meat/Poultry 163022.359088898
5 Seafood 131261.73742485
6 Condiments 106047.084989548
7 Produce 99984.5800685882
8 Grains/Cereals 95744.587474823


2. Checking Product Stock and Order Status during Peak Hours

SQL Server Query 2

            
 SELECT  
s.CompanyName AS Supplier,
SUM(p.UnitsInStock) AS TotalUnitsInStock,
SUM(p.UnitsOnOrder) AS TotalUnitsOnOrder
FROM Suppliers s WITH (NOLOCK)
JOIN Products p WITH (NOLOCK) ON s.SupplierID = p.SupplierID
GROUP BY s.CompanyName
ORDER BY s.CompanyName;

Create SQL query with SqlQueryBuilder 2

            
 var (sql2, parameters2) = new SqlQueryBuilder()  
.Select()
.Column("s.CompanyName","Supplier")
.Column(new SUM(new Column("p.UnitsInStock")), "TotalUnitsInStock")
.Column(new SUM(new Column("p.UnitsOnOrder")), "TotalUnitsOnOrder")
.From("Suppliers","s", new List<IHint>() { new NOLOCK() })
.Join(new List<IJoin>()
{
new INNERJOIN().TableName("Products","p", new List<IHint>() { new NOLOCK() })
.On(new Column("s.SupplierID").Equale(new Column("p.SupplierID")))
})
.GroupBy(new GroupBy("s.CompanyName"))
.OrderBy(new OrderBy().SetColumnAscending("s.CompanyName"))
.Build();

Query build by SqlQueryBuilder 2

            
SELECT s.CompanyName AS Supplier,
       SUM(p.UnitsInStock) AS TotalUnitsInStock,
       SUM(p.UnitsOnOrder) AS TotalUnitsOnOrder
FROM Suppliers AS s WITH (NOLOCK)
     INNER JOIN
     Products AS p WITH (NOLOCK)
     ON s.SupplierID = p.SupplierID
GROUP BY s.CompanyName
ORDER BY s.CompanyName ASC;


            
        

Parameters (If used)

Name Value

Query Results 2:

  Supplier TotalUnitsInStock TotalUnitsOnOrder
1 Aux joyeux ecclésiastiques 86 0
2 Bigfoot Breweries 183 0
3 Cooperativa de Quesos 'Las Cabras' 108 30
4 Escargots Nouveaux 62 0
5 Exotic Liquids 69 110
6 Forêts d'érables 130 0
7 Formaggi Fortini s.r.l. 23 110
8 Gai pâturage 98 0
9 G'day, Mate 58 0
10 Grandma Kelly's Homestead 141 0
11 Heli Süßwaren GmbH & Co. KG 140 0
12 Karkki Oy 132 60
13 Leka Trading 70 10
14 Lyngbysild 100 70
15 Ma Maison 136 0
16 Mayumi's 98 0
17 New England Seafood Cannery 208 0
18 New Orleans Cajun Delights 133 100
19 Nord-Ost-Fisch Handelsgesellschaft mbH 10 0
20 Norske Meierier 164 0
21 Pasta Buttini s.r.l. 57 10
22 Pavlova, Ltd. 110 10
23 PB Knäckebröd AB 165 0
24 Plutzer Lebensmittelgroßmärkte AG 205 80
25 Refrescos Americanas LTDA 20 0
26 Specialty Biscuits, Ltd. 74 50
27 Svensk Sjöföda AB 224 50
28 Tokyo Traders 64 20
29 Zaanse Snoepfabriek 51 70


3. Using NOLOCK in a Subquery for "Approximate" Comparisons

SQL Server Query 3

            
 SELECT  
p.ProductName,
p.UnitPrice,
c.CategoryName,
(SELECT AVG(p2.UnitPrice) FROM Products p2 WITH (NOLOCK) WHERE p2.CategoryID = p.CategoryID) AS AverageCategoryUnitPrice
FROM Products p WITH (NOLOCK)
JOIN Categories c WITH (NOLOCK) ON p.CategoryID = c.CategoryID
WHERE p.UnitPrice > (SELECT AVG(p3.UnitPrice) FROM Products p3 WITH (NOLOCK) WHERE p3.CategoryID = p.CategoryID) * 1.5 -- 50% higher than category average
ORDER BY p.UnitPrice DESC;

Create SQL query with SqlQueryBuilder 3

            
 var (sql3, parameters3) = new SqlQueryBuilder()  
.Select()
.Columns("p.ProductName","p.UnitPrice","c.CategoryName")
.Column(new SqlQueryBuilder().Select()
.Column(new AVG(new Column("p2.UnitPrice"))).From("Products","p2", new List<IHint>() { new NOLOCK() })
.Where(new Where(new Column("p2.CategoryID").Equale(new Column("p.CategoryID"))))
, "AverageCategoryUnitPrice")
.From("Products", "p", new List<IHint>() { new NOLOCK() })
.Join(new List<IJoin>()
{
new INNERJOIN().TableName("Categories", "c", new List<IHint>() { new NOLOCK() })
.On(new Column("p.CategoryID").Equale(new Column("c.CategoryID")))
})
.Where(new Where(new Column("p.UnitPrice").GreaterThan(
new ColumnArithmatic().StartBracket(
new SqlQueryBuilder().Select()
.Column(new AVG(new Column("p3.UnitPrice")))
.From("Products","p3", new List<IHint>() { new NOLOCK() })
.Where(new Where(new Column("p3.CategoryID").Equale(new Column("p.CategoryID")))))
.MULTIPLY(1.5)
)))
.OrderBy(new OrderBy().SetColumnDescending("p.UnitPrice"))
.Build();

Query build by SqlQueryBuilder 3

            
SELECT p.ProductName,
       p.UnitPrice,
       c.CategoryName,
       (SELECT AVG(p2.UnitPrice)
        FROM Products AS p2 WITH (NOLOCK)
        WHERE p2.CategoryID = p.CategoryID) AS AverageCategoryUnitPrice
FROM Products AS p WITH (NOLOCK)
     INNER JOIN
     Categories AS c WITH (NOLOCK)
     ON p.CategoryID = c.CategoryID
WHERE p.UnitPrice > (SELECT AVG(p3.UnitPrice)
                     FROM Products AS p3 WITH (NOLOCK)
                     WHERE p3.CategoryID = p.CategoryID) * @pMAIN_2509031324227801800
ORDER BY p.UnitPrice DESC;


            
        

Parameters (If used)

Name Value
@pMAIN_2509031324227801800 1.5

Query Results 3:

  ProductName UnitPrice CategoryName AverageCategoryUnitPrice
1 Côte de Blaye 263.5000 Beverages 37.9791
2 Thüringer Rostbratwurst 123.7900 Meat/Poultry 54.0066
3 Mishi Kobe Niku 97.0000 Meat/Poultry 54.0066
4 Sir Rodney's Marmalade 81.0000 Confections 25.1600
5 Carnarvon Tigers 62.5000 Seafood 20.6825
6 Raclette Courdavault 55.0000 Dairy Products 28.7300
7 Manjimup Dried Apples 53.0000 Produce 32.3700
8 Tarte au sucre 49.3000 Confections 25.1600
9 Schoggi Schokolade 43.9000 Confections 25.1600
10 Vegie-spread 43.9000 Condiments 23.0625
11 Northwoods Cranberry Sauce 40.0000 Condiments 23.0625
12 Gnocchi di nonna Alice 38.0000 Grains/Cereals 20.2500
13 Wimmers gute Semmelknödel 33.2500 Grains/Cereals 20.2500


4. Using multi table hints (to be used under transaction)

SQL Server Query 4

            
 BEGIN TRANSACTION;  
SELECT
OD.OrderID,
OD.ProductID,
OD.Quantity,
OD.UnitPrice,
OD.Discount
FROM [Order Details] AS OD WITH (NOLOCK, INDEX(PK_Order_Details)) -- Read uncommitted, force clustered index scan
INNER JOIN Orders O ON O.OrderID = OD.OrderID
WHERE O.OrderDate >= DATEADD(day, -10000, GETDATE()); -- Assuming OrderDate is in [Order Details] or join to Orders for it
ROLLBACK TRANSACTION

Create SQL query with SqlQueryBuilder 4

            
 var (sql4, parameters4) = new SqlQueryBuilder()  
.Select()
.Columns("OD.OrderID","OD.ProductID","OD.Quantity","OD.UnitPrice","OD.Discount")
.From("[Order Details]","OD", new List<IHint>() { new NOLOCK(), new INDEX().SetValues("PK_Order_Details") })
.Join(new List<IJoin>()
{
new INNERJOIN().TableName("Orders","O")
.On(new Column("O.OrderID").Equale(new Column("OD.OrderID")))
})
.Where(new Where(new Column("O.OrderDate").GreaterThanOrEqualeTo(new DATEADD(SqlDateInterval.day, -10000, new GETDATE()))))
.Build();

Query build by SqlQueryBuilder 4

            
SELECT OD.OrderID,
       OD.ProductID,
       OD.Quantity,
       OD.UnitPrice,
       OD.Discount
FROM [Order Details] AS OD WITH (NOLOCK, INDEX (PK_Order_Details))
     INNER JOIN
     Orders AS O
     ON O.OrderID = OD.OrderID
WHERE O.OrderDate >= DATEADD(day, @pMAIN_2509031324227954150, GETDATE());


            
        

Parameters (If used)

Name Value
@pMAIN_2509031324227954150 -10000

Query Results 4:

  OrderID ProductID Quantity UnitPrice Discount
1 11034 21 15 10.0000 0.1
2 11034 44 12 19.4500 0
3 11034 61 6 28.5000 0
4 11035 1 10 18.0000 0
5 11035 35 60 18.0000 0
6 11035 42 30 14.0000 0
7 11035 54 10 7.4500 0
8 11036 13 7 6.0000 0
9 11036 59 30 55.0000 0
10 11037 70 4 15.0000 0
11 11038 40 5 18.4000 0.2
12 11038 52 2 7.0000 0
13 11038 71 30 21.5000 0
14 11039 28 20 45.6000 0
15 11039 35 24 18.0000 0
16 11039 49 60 20.0000 0
17 11039 57 28 19.5000 0
18 11040 21 20 10.0000 0
19 11041 2 30 19.0000 0.2
20 11041 63 30 43.9000 0
21 11042 44 15 19.4500 0
22 11042 61 4 28.5000 0
23 11043 11 10 21.0000 0
24 11044 62 12 49.3000 0
25 11045 33 15 2.5000 0
26 11045 51 24 53.0000 0
27 11046 12 20 38.0000 0.05
28 11046 32 15 32.0000 0.05
29 11046 35 18 18.0000 0.05
30 11047 1 25 18.0000 0.25
31 11047 5 30 21.3500 0.25
32 11048 68 42 12.5000 0
33 11049 2 10 19.0000 0.2
34 11049 12 4 38.0000 0.2
35 11050 76 50 18.0000 0.1
36 11051 24 10 4.5000 0.2
37 11052 43 30 46.0000 0.2
38 11052 61 10 28.5000 0.2
39 11053 18 35 62.5000 0.2
40 11053 32 20 32.0000 0
41 11053 64 25 33.2500 0.2
42 11054 33 10 2.5000 0
43 11054 67 20 14.0000 0
44 11055 24 15 4.5000 0
45 11055 25 15 14.0000 0
46 11055 51 20 53.0000 0
47 11055 57 20 19.5000 0
48 11056 7 40 30.0000 0
49 11056 55 35 24.0000 0
50 11056 60 50 34.0000 0
51 11057 70 3 15.0000 0
52 11058 21 3 10.0000 0
53 11058 60 21 34.0000 0
54 11058 61 4 28.5000 0
55 11059 13 30 6.0000 0
56 11059 17 12 39.0000 0
57 11059 60 35 34.0000 0
58 11060 60 4 34.0000 0
59 11060 77 10 13.0000 0
60 11061 60 15 34.0000 0
61 11062 53 10 32.8000 0.2
62 11062 70 12 15.0000 0.2
63 11063 34 30 14.0000 0
64 11063 40 40 18.4000 0.1
65 11063 41 30 9.6500 0.1
66 11064 17 77 39.0000 0.1
67 11064 41 12 9.6500 0
68 11064 53 25 32.8000 0.1
69 11064 55 4 24.0000 0.1
70 11064 68 55 12.5000 0
71 11065 30 4 25.8900 0.25
72 11065 54 20 7.4500 0.25
73 11066 16 3 17.4500 0
74 11066 19 42 9.2000 0
75 11066 34 35 14.0000 0
76 11067 41 9 9.6500 0
77 11068 28 8 45.6000 0.15
78 11068 43 36 46.0000 0.15
79 11068 77 28 13.0000 0.15
80 11069 39 20 18.0000 0
81 11070 1 40 18.0000 0.15
82 11070 2 20 19.0000 0.15
83 11070 16 30 17.4500 0.15
84 11070 31 20 12.5000 0
85 11071 7 15 30.0000 0.05
86 11071 13 10 6.0000 0.05
87 11072 2 8 19.0000 0
88 11072 41 40 9.6500 0
89 11072 50 22 16.2500 0
90 11072 64 130 33.2500 0
91 11073 11 10 21.0000 0
92 11073 24 20 4.5000 0
93 11074 16 14 17.4500 0.05
94 11075 2 10 19.0000 0.15
95 11075 46 30 12.0000 0.15
96 11075 76 2 18.0000 0.15
97 11076 6 20 25.0000 0.25
98 11076 14 20 23.2500 0.25
99 11076 19 10 9.2000 0.25
100 11077 2 24 19.0000 0.2
101 11077 3 4 10.0000 0
102 11077 4 1 22.0000 0
103 11077 6 1 25.0000 0.02
104 11077 7 1 30.0000 0.05
105 11077 8 2 40.0000 0.1
106 11077 10 1 31.0000 0
107 11077 12 2 38.0000 0.05
108 11077 13 4 6.0000 0
109 11077 14 1 23.2500 0.03
110 11077 16 2 17.4500 0.03
111 11077 20 1 81.0000 0.04
112 11077 23 2 9.0000 0
113 11077 32 1 32.0000 0
114 11077 39 2 18.0000 0.05
115 11077 41 3 9.6500 0
116 11077 46 3 12.0000 0.02
117 11077 52 2 7.0000 0
118 11077 55 2 24.0000 0
119 11077 60 2 34.0000 0.06
120 11077 64 2 33.2500 0.03
121 11077 66 1 17.0000 0
122 11077 73 2 15.0000 0.010
123 11077 75 4 7.7500 0
124 11077 77 2 13.0000 0